I have a big problem with a calculation on my form or lets say it works in a strange way.
Well, I guess it had to do with the order they are calculated.
There are two fields (date) and one field (number) which calculates the difference of the two date fields as days.
Field 1: 2018.01.26
Field 2: 2018.01.28
Field 3: 2
Field 4: 70€
That works correctly and they are updated immediatly when I'll change the date values. So far so good.
Then I've created 5 more fields format is number.
Where I can put more values into it to create a SubTotal.
The Subtotal would look like this:
140€ (which is 2x 70€ for the example above)
The SubTotal is created like
Then I've also a Checkbox (Discount Yes/No) default to No.
When checked I display another Field where you can put a value into which will be substracted from the SubTotal field.
Now the Problem for the SubTotal field in the calculation event I placed the following:
Sum1, Sum2, Sum3, Sum4, Sum5
This works for the very first time but when I'll late change one of the date fields SubTotal isn't calculated anymore. Now the strange thing if I'll click the Discount checkbox 2 times the value for SubTotal is calculated correctly (I know that sounds a bit confusing at all.)
So my question would be ignoring the fact of possible wrong order for the calculation of the fields is there a way lets say a refresh I could send to the whole form to get the correct calculation? Well, or in other words what event happens when I check / uncheck the Discount checkbox because then it works? So maybe I can do the same from code to refresh the form.
Yes, you need to fix the fields calculation order. You can do it by clicking on Tools, Prepare Form and then click on "More" at the right side of the window and then on "Set Field Calculation Order".
So my guessing was right but that does not answer what happens when clicking the Discount checkbox 2 times.
Because each time you change the value of any field in the file it triggers the calculations of all fields, and the second time you do it the fields already have their correct values, so the result is correct.
Why not change the calculation order? That's the best approach and it will not affect anything else in the file. Any other solution is problematic.
Each field has a calcOrderIndex property. You can use a script to set the order of all the fields by setting this value.
One can place a custom calculation in the field one wants calculated last that performs all the calculations and set the resulting field values in the order one wants the calculation performed and there is not issue with the order in which Acrobat adds fields to the calculation order.
It is also possible to highlight multiple field names in the Set Calculation Order window and move them up or down as a group.
Ok I'll see I was hoping for something like this.refresh....... to refresh the whole form at once.
No problem I've changed the order now works as suggested.
There is the Doc.calculateNow() method that forces calculation but that won't help if the calculation order is off. But honestly, the more calculated fields you have, the more it makes sense to remove the calculations from the fields themselves and consolidate all of the calculations into a single, hidden, field that updates everything... much easier to manage.
Thanks a bunch! I'm really thankful.
Yes that make sense to me.