Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Custom calculation script works initially but not after reopening document

Community Beginner ,
Mar 08, 2023 Mar 08, 2023

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);

TOPICS
PDF forms
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2023 Mar 08, 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 09, 2023 Mar 09, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2023 Mar 09, 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.

 

JSPrefs_ShowConsole.jpg

 

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 09, 2023 Mar 09, 2023
LATEST

Thanks, I will give this a shot!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines