How to Fix Acrobat Form Calculation Errors with Checkboxes and JavaScript
Question details
The user needs to set up a calculation script in a PDF form that conditionally adds a value based on a checkbox state, while avoiding 'getField is null' and undefined variable errors.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Building an interactive PDF form where a checkbox determines whether a bonus (like a proficiency bonus) is added to a base modifier field.
- Observed behavior
- The custom JavaScript calculation fails to run properly, throwing errors such as 'getField is null' and 'StrMod is not defined'.
Ensure you have the exact names of all form fields (e.g., 'StrMod', 'ProficiencyBonus', and 'StrengthSavingThrow') documented, as JavaScript field references in PDFs are strictly case-sensitive.
Use a Custom Calculation Script with Exact Field References
Correctly reference your form fields using getField() inside a self-executing function to prevent scope errors and missing variable definitions.
When calculating values conditionally based on a checkbox, you must ensure your script targets the right fields and runs specifically as a calculation script on the result field. Wrapping the script in an immediately invoked function expression (IIFE) is a best practice to avoid creating unnecessary document-global variables that can conflict with other form elements.
Right-click the result field where the calculated total should appear and select 'Properties'.
Click on the 'Calculate' tab at the top of the properties window and choose the 'Custom calculation script' radio button.
Click 'Edit' and start your script by fetching the field values as numbers. Use: var StrMod = +this.getField("StrMod").value; and var ProficiencyBonus = +this.getField("ProficiencyBonus").value; (The + sign converts the text value to a number).
Add a conditional statement to check if the checkbox is checked. For example: if (this.getField("StrengthSavingThrow").value !== "Off") { event.value = StrMod + ProficiencyBonus; }
Define what happens if the checkbox is unchecked by adding: else { event.value = StrMod; }
Ensure all this code is wrapped inside (function () { ... })(); to prevent scope issues. Click 'OK' to save your script, then close the properties window to test the checkbox.

Edit and Manage Interactive PDF Forms Easily with WPS Office
WPS Office includes a powerful PDF Editor that allows you to seamlessly view, fill, and manage interactive PDF forms without dealing with complex scripting troubleshooting. It serves as an excellent all-in-one suite for processing standard form documents.
- 1. Open WPS Office: Launch the WPS Office application on your computer and click on 'PDF Edit' from the home screen.
- 2. Load your PDF Form: Browse and open the interactive PDF file containing your form fields, checkboxes, and calculation scripts.
- 3. Fill and Save seamlessly: Use the built-in form recognition to fill in the data, toggle checkboxes, and let standard embedded PDF calculations run natively. Save your completed document securely.

Frequently Asked Questions
Why do I get a 'getField is null' error in my PDF form?
This error occurs when the JavaScript engine cannot find a form field with the exact name specified in your getField command. Check for typos, mismatched capitalization, or accidental trailing spaces in the field name inside your form properties.
How do I get the value of a checkbox in a PDF script?
A checkbox typically returns the export value you assigned to it (e.g., 'Yes' or 'On') when checked, and the string 'Off' when unchecked. You can retrieve its current state using this.getField('YourCheckboxName').value in your script.
Should I put the calculation script in the checkbox or the total field?
Calculation scripts should always be placed in the 'Calculate' tab of the result field (the target field where the total appears), not as a custom action within the checkbox itself. The script in the result field will automatically trigger when the checkbox state changes.




