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

JavaScript for simple adding and subtracting

Community Beginner ,
Sep 22, 2025 Sep 22, 2025

ADD THREE FIELDS AND SUBTRACT ANOTHER FIELD TO GET A GRAND TOTAL. CAN SOMEONE PLEASE HELP ME. THANKS

TOPICS
JavaScript , PDF forms
271
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 ,
Sep 22, 2025 Sep 22, 2025

I answered you in another post, you can't use that in custom calculation script, use the same under simplified field notation or use this in custom calculation script:

var a = Number(this.getField("TOTALONE").valueAsString);
var b = Number(this.getField("TOTALTWO").valueAsString);
var c = Number(this.getField("TOTALTHREE").valueAsString);
var d = Number(this.getField("LESSADVANCES").valueAsString);

event.value = a+b+c-d;
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 ,
Sep 22, 2025 Sep 22, 2025

So, does the a+b=c+d equal the GrandTotal

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 ,
Sep 22, 2025 Sep 22, 2025

This is your form. You are the one who decides how the calculation works. Nesa was just guessing about the names of the fields on your form based on the labels used on the form. You asked about adding 3 numbers and subtracting one number.  That is exactly the code that was provided. 

In the context of scripting. "a+b=c+d" makes no sense.  I'm not sure what you are trying to do with it. Go with the code Nesa provided, but change the field names to match the fields on the form.

If you'd like to learn about writing calculation script in Acrobat, see these links:

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

 

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 ,
Sep 22, 2025 Sep 22, 2025

Thank you....I'm still learning.

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 ,
Sep 22, 2025 Sep 22, 2025

Thank you.  I don't know what I'm doing wrong, but still cannot get the calculation to give a total in the GrandTotal field.  I changed the a+b+c-d to match my field names.

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 ,
Sep 22, 2025 Sep 22, 2025

Thank you for your help...but I still do get a total in the GRAND TOTAL field.

Can you help me with that?  Do I need a calculate button?  Which I have no idea on how to creat.

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 ,
Sep 22, 2025 Sep 22, 2025

Would you mind looking at the calculation for the field GRANDTOTAL?

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 ,
Sep 22, 2025 Sep 22, 2025

Here's the correct calculation script

var a = Number(this.getField("TotalOne").valueAsString);
var b = Number(this.getField("TotalTwo").valueAsString);
var c = Number(this.getField("TotalThree").valueAsString);
var d = Number(this.getField("LESSADVANCES").valueAsString);

event.value = a+b+c-d;

 

Field Names must be verbatim, including the case.  And the last line, which is the calculation, uses the variables defined above it.  

 

 

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 Expert ,
Sep 24, 2025 Sep 24, 2025

Hello @Tori36186819mjz7 , 

 

It's important to understand that Adobe Acrobat uses a JavaScript core interpreter to perform various systematic actions and arithmetic calculations.

 

As @Nesa Nurani  and @Thom Parker  pointed out, like any programming language, particularly Acrobat JavaScript, all arithmetic formulas you create must adhere to specific expressions and Acrobat JavaScript rules to achieve the intended outcome.

 

I think you were specifically talking about using a Simplified Field Notation (SFN), which is distinct from the solutions that have already been shared since your original post mentions JavaScript. It creates a bit of confusion for us trying to understand your requirement.

In this context, writing a simplified field notation requires a different syntax than that of a Custom Calculation Script. Additionally, in your shared PDF you disregarded the advice that @try67  and @PDF Automation Station  already shared with you in your intial post: https://community.adobe.com/t5/acrobat-discussions/simple-multiplication-of-two-fields/td-p/15516016

 

The names of the fields that you will refer to in your scripts need to be spelled precisely as you wrote them when you created them. Currently, you spelled all of the field names in your SFN in Upper Case, but your fields are spelled  Case Sensitive. I've noticed in your shared document that you also have the same error in other total fields.

 

Note that, with the exception of the field "LESSADVANCES", your SFN syntax should be expressed exaclty as :

 

TotalOne + TotalTwo + TotalThree - LESSADVANCES

 

Additionally, because SFN mostly resembles arithmetic expressions (rather than JavaSript scripting expressions), if you are employing SFN expressions, you should get in the habit of expressing arithmetic formulas like so:

 

(TotalOne + TotalTwo + TotalThree) - LESSADVANCES

 

Employing parenthesis  ( )  in arithmetic formulas greatly helps someone to visually grasp the execution order of an equation if they would be reviewing your work; it just makes it a lot easier to understand in a human-readable form what needs to happen first in an equation.

 

Also, you've used the Custom Calculation Script to declare the value of "GrandTotal" using the SFN syntax in it.

 

Should you decide to keep your work entirely as SFN, move the expresion of  TotalOne + TotalTwo + TotalThree - LESSADVANCES  to the Simplified Field Notation item under the Claculate tab (like you've done before). Otherwise, you may copy and paste the scripts provided by @Nesa Nurani and @Thom Parker and use them to test your results as Custom Calcution Scripts (not SFN); both methods should trigger the same result values.

 

Here is an updated PDF from your original work with some improvements:  Travel-Expense-2025 

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
New Here ,
Sep 24, 2025 Sep 24, 2025
LATEST

Thanks for your advice.  I'm still learning!

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