Skip to main content
Participating Frequently
March 27, 2025
Answered

Java Script Formula Help

  • March 27, 2025
  • 2 replies
  • 2272 views

I am trying to write a formual to sum up fields based on a another field.

 

Column 2  is named "secured or unsecured" and Column 2  is the called Current Bal. 

 

I need to have a formula that adds up unsecured and then adds up secured. 

 

I did get a formual for the first column to work but I dont know how to get it to look at all those columns and add them based on secured or unsecured 

 

This is the formula that worked but it only looked at the first row in that column. I need it to look at all five and sum the secured and the unsecured seperate.

 

var f = this.getField("E Current Bal_1");
var g = this.getField("E Secured orUnsecured_1").value;
if (g == "Secured") event.value = f.value;
else if (g == "Unsecured") event.value = 0;

 

 

Correct answer Nesa Nurani

Still not working 


1. You have both first two fields "E Secured orUnsecured_" named with number 2, should replace number to 1 on the first field.

2. You placed script in two fields, one field is enough.

3. In the script you changed variable names to uppercase but left the last two to lower case.

4. In your dropdown choices are not "Secured" and "Unsecured" they are "Secured " and "Unsecured "
You can fix those issue by yourself or here is the file where I made those changes:

https://drive.google.com/file/d/11p9cpR90SppXn7nXETGb3rqUZ9gW-Cok/view?usp=sharing 

 

2 replies

Nesa Nurani
Community Expert
Community Expert
March 27, 2025

You wish to sum up "Current Bal" if corresponding "Secured or unsecured" have word "Secured"?

Try this as custom calculation script of 'Total' field:

var total = 0;

for(var i=1; i<=5; i++){
 var f = Number(this.getField("E Current Bal_"+i).valueAsString);
 var g = this.getField("E Secured orUnsecured_"+i).valueAsString;

 if(g == "Secured")
  total += f;}

event.value = total;

 

Participating Frequently
March 27, 2025

This didnt work. I have a two seperate line items where I need it to add totals together from current balance column based on what they choose in the drop down .  If they choose secured it should total all that they choose secured on and same thing total unsecured

 

Here is a better screenshot

These are the line items where I will have a formula

 

Nesa Nurani
Community Expert
Community Expert
March 27, 2025

Let's say the names of the two fields are "Secure" and "Unsecure" put this in one of the fields as custom calculation script:

var secured = 0;
var unsecured = 0;

for(var i=1; i<=5; i++){
 var f = Number(this.getField("E Current Bal_"+i).valueAsString);
 var g = this.getField("E Secured orUnsecured_"+i).valueAsString;

 if(g == "Secured")
  secured += f;
 if(g == "Unsecured")
  unsecured += f;}

this.getField("Secured").value = secured;
this.getField("Unsecured").value = unsecured;
Thom Parker
Community Expert
Community Expert
March 27, 2025

So, is the "E Secured orUnsecured" (colum 2) a dropdown where the user can select either "secured" or "unsecured"? 

And is the script placed in the "Custom Calculation" script for the "E_CurrentBal_Total" fields?

If so, then your script is close. 

Try this:

var cSelect, nSum = 0;
for(var i=1;i<=5;i++)
{
   cSelect= this.getField("E Secured orUnsecured_"+ i).value;
   if (cSelect == "Secured")
       nSum +=  this.getField("E Current Bal_1").value;
}
event.value = nSum;

 

This script only sums the values marked as "Secured", since you only provided a single total field.  Are there other fields where the different sums are supposed to go?  

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Nesa Nurani
Community Expert
Community Expert
March 27, 2025

There is a small error in script, replace "E Current Bal_1" with "E Current Bal_"+i