5 Replies Latest reply on Jun 17, 2014 5:10 PM by MatLac

# I need a custom calculation script that takes the target field name and extrapolates the names of the other fields from it, and then uses their values in the calculation.

Here is what I am trying to accomplish:

I have a long Excel price list that I converted to pdf and turned into a fillable pdf order form.  Each row provides information for 1 product and allows the customer to enter an order qty. At the end of each row is a calculation field that calculates the product of the price x qty.

SO, for example I have fields such as this:   Price1     Qty1      Total1     and the calculation is set as:  Calculation is the product of Price1, Qty1

I have taken those fields and, using the command "Place Multiple Fields", have populated each row of the pdf with additional fields.

Now I have the following fields:

In row 1:   Price1.0.0    Qty1.0.0      Total1.0.0

In row 2:   Price1.0.1     Qty1.0.1     Total1.0.1

In row 3:   Price1.0.3     Qty1.0.3      Total1.0.3, etc.

The only thing is that the calculation in these new total fields have the same information:    Calculation is the product of Price1, Qty1.

I need each row to have a different calculation (eg For Total1.0.1:  Calculation is the product of Price 1.0.1, Qty1.0.1, etc.)

Is there javascript code that would update the calculations for me so that if I use the command "Place Multiple Fields" - each of the new Total fields would contain update calculations?

I hope this makes sense.  All help is appreciated.  Thank you

• ###### 1. Re: I need a custom calculation script that takes the target field name and extrapolates the names of the other fields from it, and then uses their values in the calculation.

The best thing to do in this situation is create a function in a document-level JavaScript file that is then called by each of the Total fields. The script can determine which row the field is on by getting the field name (event.target.name" and splitting it at the decimal point into an array:

// Function in ducument JavaScript

function calcTotal() {

// Get the field name array of the field that triggered this script

var fna = event.target.name.split(".");

// Get the row, which is the third element of the array

var row = fna[2];

// Now that we have the row, get the other fields and perform the calculation

event.value = getField("Price1.0." + row).value * getField("Qty.0." + row).value;

}

So each Total field would call this same function.

• ###### 2. Re: I need a custom calculation script that takes the target field name and extrapolates the names of the other fields from it, and then uses their values in the calculation.

Thank you, George,

Would i need to still put a calculation in the first Total field?

Would I put this javascript into the the document and then do the "Place Multiple Fields" ?

Thank you for your help!

• ###### 3. Re: I need a custom calculation script that takes the target field name and extrapolates the names of the other fields from it, and then uses their values in the calculation.

You would add the function to a document-level JavaScript and call it in the calculate event of each total field. You can do this either before or after you "Place Multiple Fields", it just won't work before you do since the field names won't be what the script is expecting, but that's not important.

• ###### 4. Re: I need a custom calculation script that takes the target field name and extrapolates the names of the other fields from it, and then uses their values in the calculation.

Thanks, George,

Could you possibly provide an example of what I'd need to put in the Calculate area to call the function?

Also, you mention I need to add this in every total field?  I was already able to set each total field to calculate - without Javascript.  But as hoping Javascript would provide a way to automatically update names of the fields inside each total field.  My forms are 250 lines long, so it would be great to find a way where I could use "Place Multiple Fields" and the field names inside each subsequent Total field would be updated.

Thanks,

Pam

• ###### 5. Re: I need a custom calculation script that takes the target field name and extrapolates the names of the other fields from it, and then uses their values in the calculation.

250 lines, lol.  You need a loop then.

// Function in document JavaScript

function calcTotal() {

for (var i = 0; i<=249; i++){

this.getField("Total1.0." + i).value = this.getField("Price1.0." + i).value * this.getField("Qty.1.0." + i).value;

}

}

Launch this script only ONCE in a custom calculate script and the script will run through all 250 lines from line 0 to line 249.

to launch it:

//put this in a calulate script

calcTotal();