Skip to main content
Participating Frequently
April 22, 2025
Question

Code Help - caluculation

  • April 22, 2025
  • 3 replies
  • 309 views

Hello, 

I am updating a form field to be able to calculate the product of one form field by another form field, however one of the fields a statis field and the other can be editted. When I try this code in the custom calculation section, it will only caluculate the first item of 10, but nothing afterwards, and any changes are not updated accordingly. I am at my wit's end and have tried all I can think of with no results. I put in validation instead of calculation and tried altering the code a few times to no avail. Any help would be greatly appreciated! 

 

"4 Funds" is a dropdown box and "Annual Income" is a static, calculated field that cannot be editted.

 

var Funds = this.getField("4 Funds").value;
var Income = this.getField("Annual Income").value;
if (Funds == 10)
event.value == Income*8.9;
else if(Funds == 15)
event.value == Income*12.4;
else if(Funds == 20)
event.value == Income*15.4;
else if(Funds == 25)
event.value == Income*18.1;
else if(Funds == 30)
event.value == Income*20.4;
else if(Funds == 35)
event.value == Income*22.4;
else if(Funds == 40)
event.value == Income*24.1;
else if(Funds == 45)
event.value == Income*25.6;
else if(Funds == 50)
event.value == Income*26.9;
else if(Funds == 55)
event.value == Income*28.1;
else if(Funds == 60)
event.value == Income*29.0;

3 replies

Participating Frequently
April 22, 2025

 

It looks like you're close to what you need, but the issue seems to be with how you're using the assignment operator (==) in your code. The == operator is a comparison operator, which is why it's not updating the value correctly. You should be using the assignment operator (=) instead.

Here’s how you can fix your code:

 

javascript
CopyEdit
var Funds = this.getField("4 Funds").value; var Income = this.getField("Annual Income").value; if (Funds == 10) event.value = Income * 8.9; else if (Funds == 15) event.value = Income * 12.4; else if (Funds == 20) event.value = Income * 15.4; else if (Funds == 25) event.value = Income * 18.1; else if (Funds == 30) event.value = Income * 20.4; else if (Funds == 35) event.value = Income * 22.4; else if (Funds == 40) event.value = Income * 24.1; else if (Funds == 45) event.value = Income * 25.6; else if (Funds == 50) event.value = Income * 26.9; else if (Funds == 55) event.value = Income * 28.1; else if (Funds == 60) event.value = Income * 29.0;
 

Key Fixes:

  1. Change == to =: In the event.value == Income * 8.9 lines, you should use = (assignment) instead of == (comparison). The correct syntax is event.value = Income * 8.9;, which assigns the calculated value to the field.

  2. Ensure Proper Field Update: When a user changes the dropdown or the "Annual Income" field, this calculation will update automatically.

Additional Tips:

  • Ensure that the field "4 Funds" and "Annual Income" are properly set up, and that the "Annual Income" field is calculated before the script runs. If "Annual Income" is calculated dynamically, make sure its calculation occurs first.

  • If you're using this code in a PDF form (like with Adobe Acrobat), ensure that the script is placed in the Custom Calculation Script of the field where you want to display the result.

Eric5C58Author
Participating Frequently
April 22, 2025

I also want to add that when running the console log I get the following error message in droves. I have no simplified calculation script, all calculations are done via custom script, and there are no fields that are referenced that do not exist. I am losing my mind trying to get this to work and I feel so dumb not knowing how to fix. 

 

TypeError: f is null
1282:byteCodeTool
TypeError: f is null
1282:byteCodeTool
TypeError: f is null
1282:byteCodeTool
TypeError: f is null
1282:byteCodeTool
TypeError: f is null
1282:byteCodeTool
TypeError: f is null
1282:byteCodeTool
TypeError: f is null
1282:byteCodeTool
TypeError: f is null
1282:byteCodeTool
TypeError: f is null
1282:byteCodeTool

Bernd Alheit
Community Expert
Community Expert
April 22, 2025

Change all

event.value ==

to

event.value =

 

Eric5C58Author
Participating Frequently
April 22, 2025

So that worked, although I tried that before to no avail, so that baffles me as to why it didn;t work before when I tried. Thank you for the simple fix!

 

My next issue is that some of the values will not refresh until after I have input a value in a different field. How do I ensure that all values/form fields are refreshed and updated after every change or input?

Bernd Alheit
Community Expert
Community Expert
April 22, 2025

Change the field calculation order.