Fix a Blank Calculated Field in Adobe Acrobat JavaScript
Question details
The user needs to display a calculated value in an Acrobat PDF form field using custom JavaScript.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- A custom calculation script references an array of values from fields named q1 through q63.
- Observed behavior
- The calculation field remains blank when variables such as q1 through q63 are referenced without being defined.
Open the PDF in Adobe Acrobat and check the exact names of the source fields and the calculated field in Prepare a Form. Confirm what each source value represents and how the final percentage should be calculated.
Read the form fields and assign the calculation result
Use this method when the script references undefined variables or does not assign its result to the calculation field.
Acrobat form field names are not automatically JavaScript variables. Retrieve each field with getField() before using its value, and assign the final result to event.value.
The example below calculates the average of valid numeric inputs from 0 to 100. Replace that formula if your definition of completion is different.
In Acrobat, open the PDF, select Prepare a Form, and open Properties for the calculated field. On the Calculate tab, select Custom calculation script and click Edit.
In the script editor, retrieve the fields by name and skip missing, blank, or invalid values. For example, use this complete script for an average of values from 0 to 100: var total = 0; var countValid = 0; for (var i = 1; i <= 63; i++) { var field = this.getField("q" + i); if (!field) continue; var raw = field.valueAsString; if (raw === null || raw.replace(/\s/g, "") === "") continue; var value = Number(raw); if (!isNaN(value) && value >= 0 && value <= 100) { total += value; countValid++; } } var percentCompletion = countValid > 0 ? total / countValid : ""; event.value = percentCompletion;
Click OK to save the script, close the field properties, and enter numeric values in the q fields. Check that the calculated field displays the expected result and remains blank when there are no valid values.

Work with PDFs and Office files in WPS Office
WPS Office is a free, lightweight alternative for everyday document work, with a familiar interface and compatibility with common Microsoft Office formats to ease migration. You can also open PDFs for review; use Acrobat to troubleshoot its custom form JavaScript.

Frequently Asked Questions
Why does referencing q1 directly leave my calculation blank?
A form field named q1 does not automatically define a JavaScript variable named q1. Retrieve it with this.getField("q1") and read its value before using it in the calculation.
Where do I add a custom calculation script in Acrobat?
Open Prepare a Form, open the calculated field's Properties, and select Calculate. Choose Custom calculation script and click Edit.
How do I prevent division by zero?
Check countValid before dividing. For example, use countValid > 0 ? total / countValid : "" to leave the result blank when no valid values are available.
Why is my result not displayed even though the calculation runs?
The calculation script must assign the result to event.value, such as event.value = percentCompletion. Also check the calculated field's format in its Properties if the displayed number is not in the expected form.




