Skip to main content
Participant
March 31, 2024
Question

Subtracting 3 form fields without continuing to minus their result when inputting other form field

  • March 31, 2024
  • 1 reply
  • 711 views

Hello, please help me.

Is there a custom calculation script for this to happen? I have 3 fields to be subtracted from the first form field and when I input other form filled they keep subtracting from each other (only the 3 form filled)

this is my calculation script:

 

var a = this.getField("3").value;

var b = this.getField("2").value;

var c = this.getField("1").value;

event.value = a;

if (a !=="" && b !=="") {event.value = a - b;}

if (a !=="" && c !=="") {event.value = a - c;} ;

;

 

 

So the final result of their difference would be on Field 3.

Please tell me where I went wrong on this one. I also tried setting their calculation order it just keeps messing up. Or is there a custom script for this to prevent from subtracting furthermore.

 

Thank you! I use adobe acrobat

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
March 31, 2024

Calculation scripts are run every time any field on the form changes. Using the value of the field in a calculation script on that field creates a feedback loop, not good.  To fix this issue you need to add another form field to hold the field "3" value and break the loop. 

 

BTW: using numbers as field names is a bad practice. Use short descriptive names. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
March 31, 2024

Thank you for your response.

 

So what you mean is , there is no way that I can calculate 2 fields to result on the first? That's why it creates a loop? 

Thom Parker
Community Expert
Community Expert
March 31, 2024

Thank you for this wonferful information.

I have another problem,

when I enter other data from the other form fields they somehow keep subtracting still from Box2 and Box3, is there any way to just minus them once?


It is possible that there are still old calculation scripts attached to those fields. Make sure that all calculations are removed, except for the two in Result1 and Result2.

In fact, a good exercise would be to create a test form that contains only those fields that are used in this calculation. Use this to test the calculations in a clean environment. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often