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

Creating Custom JavaScript Calculation

Community Beginner ,
Mar 08, 2023 Mar 08, 2023

Copy link to clipboard

Copied

Hello,

 

I am having a difficult time calculating the total of multiple fields and having that total display in the total field. The form I am working on has the ability to spawn a new page as well, the image below is a spawned page.  

 

Screenshot 2023-03-08 113729.png

 

I've tried using the following code as a custom calculation but no matter what I do the CurrencyTotal field remains blank.

//define strPrefix
var strPrefix = "";
var aFieldName = event.targetName.split(".");
If(aFieldName.length > 2);  {
strPreFix = aFieldName[0] + "." + aFieldName[1] + ".";}

//give each field a temp name and bring in its value
var nA = this.getField(strPrefix + "Hundreds").value;
var nB = this.getField(strPrefix + "Fifties").value;
var nC = this.getField(strPrefix + "Twenties").value;
var nD = this.getField(strPrefix + "Tens").value;
var nE = this.getField(strPrefix + "Fives").value;
var nF = this.getField(strPrefix + "Twos").value;
var nG = this.getField(strPrefix + "Ones").value;

//perform calc
var nSum = nA + nB + nC + nD + nE + nF + nG;

//assign total to correct field
event.value = nSum;

 

Any help is greatly appreciated!!

 

Thanks in advance! 🙂 

TOPICS
Acrobat SDK and JavaScript

Views

2.3K

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Mar 09, 2023 Mar 09, 2023

The null error is happening in a different script than the println. We know this because exceptions exit the script. 

The only way to know for sure which field name is returning null is to place "console.println" code before the line where the exception is thrown. 

 

It looks like it is the "GrandTotal" field contains the script that is causing the exception. 

The fact that this field is throwing the error before the "CurrencyTotal" field prints the report, means that your calculations are out o

...

Votes

Translate

Translate
Community Expert ,
Mar 08, 2023 Mar 08, 2023

Copy link to clipboard

Copied

There are a couple fatal errors in your script. The first thing to look at when you enter a script in a PDF is the Console Window. Which is were the first error would be reported. 

Here's an update to your script:

//define strPrefix
var strPrefix = "";
var aFieldName = event.targetName.split(".");
if(aFieldName.length > 2) 
    strPreFix = aFieldName[0] + "." + aFieldName[1] + ".";

//give each field a temp name and bring in its value
var nA = this.getField(strPrefix + "Hundreds").value;
var nB = this.getField(strPrefix + "Fifties").value;
var nC = this.getField(strPrefix + "Twenties").value;
var nD = this.getField(strPrefix + "Tens").value;
var nE = this.getField(strPrefix + "Fives").value;
var nF = this.getField(strPrefix + "Twos").value;
var nG = this.getField(strPrefix + "Ones").value;

//perform calc
event.value = nA + nB + nC + nD + nE + nF + nG;

 

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

Votes

Translate

Translate

Report

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 08, 2023 Mar 08, 2023

Copy link to clipboard

Copied

Hi Thom,

 

That worked but it didn't add the values together properly. For example if I had $20 in one field and $10 in another, the Currency Total field was $2,010. It treated the values as characters rather than numbers. So I was able to modify the code to:

//define strPrefix
var strPrefix = "";
var aFieldName = event.targetName.split(".");
if(aFieldName.length > 2) 
    strPreFix = aFieldName[0] + "." + aFieldName[1] + ".";

//give each field a temp name and bring in its value
var nA = Number(this.getField(strPrefix + "Hundreds").valueAsString);
var nB = Number(this.getField(strPrefix + "Fifties").valueAsString);
var nC = Number(this.getField(strPrefix + "Twenties").valueAsString);
var nD = Number(this.getField(strPrefix + "Tens").valueAsString);
var nE = Number(this.getField(strPrefix + "Fives").valueAsString);
var nF = Number(this.getField(strPrefix + "Twos").valueAsString);
var nG = Number(this.getField(strPrefix + "Ones").valueAsString);

//perform calc
event.value = nA + nB + nC + nD + nE + nF + nG;

 

However, this code only works on my template - nothing happens when I spawn a page. 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Excellent Catch!!  Now it's time for some debug.

Is anything reported in the console window?

 

Add a console.println() statement to your code to see what's going on inside the calculation:

Here's a video tutorial on the topic:

     https://www.pdfscripting.com/public/images/video/AcroJSIntro/AcroJSIntro_ConsoleWindow.cfm

 

Here's the modified script with debug code added:

var strPrefix = "";
var aFieldName = event.targetName.split(".");
if(aFieldName.length > 2) 
    strPreFix = aFieldName[0] + "." + aFieldName[1] + ".";

// Report prefix for debug purposes
console.println("Name:" + event.targetName + "  Prefix: " + strPrefix);

//give each field a temp name and bring in its value
var nA = Number(this.getField(strPrefix + "Hundreds").valueAsString);
var nB = Number(this.getField(strPrefix + "Fifties").valueAsString);
var nC = Number(this.getField(strPrefix + "Twenties").valueAsString);
var nD = Number(this.getField(strPrefix + "Tens").valueAsString);
var nE = Number(this.getField(strPrefix + "Fives").valueAsString);
var nF = Number(this.getField(strPrefix + "Twos").valueAsString);
var nG = Number(this.getField(strPrefix + "Ones").valueAsString);

//perform calc
event.value = nA + nB + nC + nD + nE + nF + nG;

 

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

This is what is returning:

DebugError.png

With the "this.getField(...) is null" error meaning a field doesn't exist, how do I determine which field it's referring to?

 

 

 

 

 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

The null error is happening in a different script than the println. We know this because exceptions exit the script. 

The only way to know for sure which field name is returning null is to place "console.println" code before the line where the exception is thrown. 

 

It looks like it is the "GrandTotal" field contains the script that is causing the exception. 

The fact that this field is throwing the error before the "CurrencyTotal" field prints the report, means that your calculations are out of order.

But thats a different issue, and for debug purposes you should remove the GrandTotal script entirely. 

 

So it looks like the "P2.CashMvmt.CurrencyTotal"  calculation is working, or at least it's not thowing an error. 

However, the printout from the console.println is slightly disturbing. Because if  "event.targetName" was split to get the prefix shown, then it should have printed out like this:

 

Name:P2.CashMvmt.CurrencyTotal  Prefix: CashMvmt.CurrencyTotal

 

I would suggest simplifying the form until the scripting is worked out. To do this, make a copy of the PDF, and then delete everything except for one of the total fields, and all the inputs that are necessary for that total. 

 

 

 

 

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

Votes

Translate

Translate

Report

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 13, 2023 Mar 13, 2023

Copy link to clipboard

Copied

Thank you Thom!

 

I was able to remove the GrandTotal script and fix the order of the calculations. I haven't had and further issues. I'm new to this so thank you for all of your help, you've taught me quite a bit!

 

Anissa E 

 

Votes

Translate

Translate

Report

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 13, 2023 Mar 13, 2023

Copy link to clipboard

Copied

LATEST

Excellent, How about marking a correct answer 🙂

 

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Spawning a page does not trigger calculation, after you spawn a page, try changing some values of a field to trigger it.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

I apologize, I meant the calculation on the spawned page is not working the same way it is on the template, even after trying to add values to each field. 

Votes

Translate

Translate

Report

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