Skip to main content
Known Participant
July 9, 2021
Answered

Formula update

  • July 9, 2021
  • 2 replies
  • 970 views

Hi,

I use this simple formula:

var valeurA = this.getField("p_52").value;
var valeurB = this.getField("p_502").value;
var valeurC = this.getField("p_33").value;
var total = (valeurA + valeurB - valeurC) * 1;
if (total < 0) {total = 0;}
event.target.value = total;

If the value of field A and B changes, the formula result will not update automatically

until I enter some value in field C (even 0). I would like it not to be necessary to enter any value in the C field to see the result of the A + B fields. Please help. Thank you very much for help!

This topic has been closed for replies.
Correct answer Bernd Alheit

Your field calculation order is wrong:

2 replies

BarlaeDC
Community Expert
Community Expert
July 9, 2021

Hi,

 

If C has no value then it might not be able to calulcate it, so the script is failing ( will see this in the console if it is the problem)

 

but you could just default the value to 0 and that would solve the problem, this can be done by setting a default value in the field or using this code

var valeurC = isNaN(this.getField("p_33").value) ? 0 : this.getField("p_33").value;
Known Participant
July 9, 2021

Unfortunately, this doesn't seem to be working. Please look at field 34 (last on the second page). Unfortunately, field 33 must be empty, and fields A + B must sum up 😞

BarlaeDC
Community Expert
Community Expert
July 9, 2021

Hi,

 

Is field A "P_52" ( this is on page 3)

 Is Field B "P_502" ( this is on page 3 also)

 

I also notice that P_502 has a calculate method on it, it may be that these are not running in the order you expect due to the fields locations, if you are looking to have calculations across pages, you may need to create your own method to do these, so that you can make sure the calculations are happening in the order you expect.

 

Currently for your text field "Text7.0.1.1.1.1.1.1.1.2" to be populated, this needs to happen

 

P_52 needs a value

P_502 needs a value

 

for P_52 to get a value the user has to have gone to page 3 and filled it in ( not a problem but we need to deal with the calculation before they have gone to page 3)

for p_502  to get a value the user has to have gone to page 3 and filled in p_500 and p_501 ( again not a problem but we need to deal with the calculation before they have gone to page 3)

 

It is also not really in a good method as there is a good chance the calculation for "Text7.0.1.1.1.1.1.1.1.2" will run before the calculation for p_502 and this means that the calculation will always be out of sync, which may be the problem.

 

If you need these to be in a specific order it might be better to make a function at the documet level, which can be called when any of the fields are updated and then this would make sure that your calculations happen in the order that is expected.

 

Please feel free to correct me if I have mis-understood how the form should function.

Bernd Alheit
Community Expert
Community Expert
July 9, 2021

Check the field calculation order.