Skip to main content
Tammy28781641mkxk
Participating Frequently
March 9, 2023
Question

Custom calculation script works initially but not after reopening document

  • March 9, 2023
  • 1 reply
  • 1528 views

I have a custom calculation script in a form field that works when I put nthe script in and close and enter data but after the form is cleared it will not work again unless I go back in and put the custom calculation script in again. I have several custom calculation scripts in the form that are working fine, so I am not sure what I have done wrong?

 

event.value = Number(this.getField("Remaining Balance 3").valueAsString) / Number(this.getField("Divide by 2").valueAsString);

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
March 9, 2023

Please check the console window for errors (Ctrl-J).  

There is one error in this code. There is a possible divide by zero condition that is not accounted for, as well as NaN conditions. 

Change the script to this. But please check the console first.

 

var cDivisor = this.getField("Divide by 2").valueAsString;
if(cDivisor && !isNaN(cDivisor) && (Number(cDivisor) > 0))
{ // Proceed with calculation
    event.value = Number(this.getField("Remaining Balance 3").valueAsString)/Number(cDivisor );
}

 

 

An NaN condition (Not a Number) is when text does not automatically convert to a number. Usually this condition is obvious, but a more subtle case is when a written number contains commas.  Commas do not convert. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Tammy28781641mkxk
Participating Frequently
March 9, 2023

Unfortunately I my version of Adobe does not support using the Ctrl-J command so I pasted this as the script and it is saying Syntaxerror: missing } in compound statement 1: at line 2

Thom Parker
Community Expert
Community Expert
March 9, 2023

That error does not match the script, so something is off.   Perhaps you missed copying the final closing bracket?

 

Regardless, all Acrobat versions have a console window, even if the button for getting to it is missing. Go to the Acrobat Preferences and find the "JavaScript" category. Select the option that displays the console(or debugger) on an error.

 

 

 

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