17 Replies Latest reply on Nov 23, 2016 7:08 AM by Pricoky

How to calculate sum of filled fields?

Hi there!

My question to you is how can I calculate the sum of certain filled fields?

For example in a PDF document I have some fields that need to be filled with name and surname and at the end of the document another field that represents the total of the "Name and surname" fields which shows how many persons are in this document. The operation shouldn't take in account blank/unfilled fields.

Now I know I could do this with the simple "sum(+)" calculation but I'd have to enter a "1" for each of this field and I'd like to avoid that.

• 1. Re: How to calculate sum of filled fields?

That's not really a sum, but more of a total.  To do it would require using a custom-made script.
Are the names of the fields consistent (like Name1, Name2, Name3, etc.), or is it just a list of unrelated fields?

If the latter then you can use this script as the custom calculation script of the text field where you want the total to appear:

```var fields = ["First Name", "Last Name"]; // etc.
var total = 0;
for (var i in fields) {
if (this.getField(fields[i]).valueAsString!="") total++;
}
event.value = total;
```
• 2. Re: How to calculate sum of filled fields?

Thank you try67, the code you provided works.

The names of the fields look like this: "Nume si prenume 1" till "Nume si prenume 79". Is there a way to "tell" the code to take in account all the other fields without having to enter each one manually?

• 3. Re: How to calculate sum of filled fields?

Yeah, that's the first option I described. In that case you can use this code:

```var total = 0;
for (var i=1; i<=79; i++) {
if (this.getField("Nume si prenume "+i).valueAsString!="") total++;
}
event.value = total;
```
• 4. Re: How to calculate sum of filled fields?

Sir you are AWSOME! THANK YOU SO MUCH!

• 5. Re: How to calculate sum of filled fields?

Hi there guys! A simple question for you. Which is the proper script to automatically fill a field with the double amount of a value entered in another field? I've already tried a few scripts but it seems that they're messing up the "total" field!

Thank you!

• 6. Re: How to calculate sum of filled fields?

event.value = Number(this.getField("Other field name goes here").value) * 2;

• 7. Re: How to calculate sum of filled fields?

OK! This works but... It is somehow messing up my TOTAL field. Don't know why. Here it is a explanation .jpej file and Here is the actual .pdf file if you could please take a look 'cause I just can't figure it out!

• 8. Re: How to calculate sum of filled fields?

You need to adjust the fields calculation order. Total must be calculated after the Valoare fields, for example.

• 9. Re: How to calculate sum of filled fields?

Hmm.. from where can I change this order?

• 11. Re: How to calculate sum of filled fields?

Found it, change it! But still when changing the values of the other fields after the total was calculated does not imediatly reflect in the "total" field nor the other to fields where the values should change according to the modified values. Wouldn't be better to use a custom calculation java script for the "total" instead of the standard calculation "sum "

• 12. Re: How to calculate sum of filled fields?

Yes, that might be a good idea... Also check the JS console for error

messages.

If it still doesn't work, share the file again.

• 13. Re: How to calculate sum of filled fields?

Ok, it took me a while to check fo errors but couldn't find one. I think the problem is with  field "B" as shown in the .jpeg file I've posted earlier. The "B" field gets it's value from field "A". This value is automatically multiplied by two. So to get a value in "B" first I'll have to enter a value in "A". Now in "Field calculation order" field "A" does not appear instead field "B" does.

To be more speficic this form will be used for eyglasses orders. A client can/could make two pairs of glasses that's why I have to similar "areas" separated by the thin red line. The forms should calculate frames price, lens(fields "A" and "B") price and montage price if it is required and get the total shown in "TOTAL lei)" field. Then there are the other two lower fields, in first I enter the advance sum that the client pays and the other field calculates the rest of money the client has to pay.  THIS is the file as it is at this moment. Don't know what else to say!

• 14. Re: How to calculate sum of filled fields?

You set the calculation order in reverse order. The lower a field is in the list, the later it will be calculated. Total should be the last field in your list, not the first...

• 15. Re: How to calculate sum of filled fields?

Thank you man! I've reversed the calculation order and now it seems to work just fine!

Oh.. just another tiny little question. Is it possible to add a button which when pressed to recalculate/refresh all the fields just to be sure there is no misscalculation mistake before printing?

• 16. Re: How to calculate sum of filled fields?

It's possible, but it should not be necessary if you set it up correctly.

If you really want to do it then use this code as the button's MouseUp event:

this.calculateNow();

• 17. Re: How to calculate sum of filled fields?

MAN YOU ARE AWSOME !!!