2 Replies Latest reply on Jun 12, 2014 2:06 PM by gkaiseril

# Javascript needed for multiple lines of the same calculations: price x quantity = total

I have received some help already but I need more details - like exactly where to place the code - and - do I place the code before using "Place Multiple Fields?  Also, does the first Total1.0 field need any calculation in it or does the area under the "Calculate" tab need to be blank?

The fields in the example below were placed using "Place Multiple Fields".  Instead of going into the Total fields one at a time and telling it to pick up the correct field names for Price and Qty for that row, I'd love a formula that was able to figure out the totals for each row.   The only thing is that when you use "Place Multiple Fields" the field names change (e.g. Price1.0, Price1.1, Price1.2, etc.)

I really need this spelled out step by step for me, if someone has time time.

Thank you x 1,000,000.

• ###### 1. Re: Javascript needed for multiple lines of the same calculations: price x quantity = total

Sorry, I didn't notice your question in the other thread. I just responded, but post again if you have any questions.

• ###### 2. Re: Javascript needed for multiple lines of the same calculations: price x quantity = total

With Hierarchical field names one can create an array of the lower field objects below a one of the levels in the hierarchy.

// Custom JavaSCript for the subtotal field;

var oHFieldTotal = this.getField("Total1");

var aHFieldTotal = oHField.getArray(); // Total field objects in an array;

var aHFieldPrice = this.getField("Price1").getArray(); // Price field objects in an array;

var aHFieldQty = this.getField("Qty1").getArray(); // Qty field objects in an array;

var nSum = 0; // sum of extended price * qty;

// process each row of fields;

for(i = 0; i < aHField.length; i++) {

aHFieldTotal[i].value = aHFieldPrice[i].value * aHFieldQty[i].value; // compute the extended value;

nSum += aHFieldTotal[i].value; // add the extended value to the running sum;

} // end for loop;

event.value = nSum; // set the field value to the running sum;