Calculating the sum total of a number Field in a Repeat Section (adding)

Quite often you will want to add up all of the numbers in a Repeat Section. For example, if you have a Repeat Section with the details of invoices on each row of the Repeat Section, and you want to find out what the total of the invoice values is for all of the invoices entered in that Repeat Section.

You can do this using a simple Script as follows:

var sum = $('[name$="Invoice_Amount_num"]').sumValues()
fld('Invoice_Total_scr').val(sum);

You need to insert this script in the Field Script Editor for the Form Field in the Repeat Section that you want to find the sum total of. In this case, the Script would be included in the Script Editor for the Field Invoice_Amount_num.

You also need to put another Field on the Form outside of the Repeat Section into which the sum total will be set. In this case, the Field is Invoice_Total_scr.

What this Script is effectively saying is: Set the variable called ‘sum’ to the total of the values in the Field ‘Invoice_Amount_num’, and then set the value of ‘Invoice_Total_scr’ to the value of ‘sum’.

Because the Script is referring to all instances of ‘Invoice_Amount_num’ on the Form, it will add up all the instances of ‘Invoice_Amount_num’ within the Repeat Section.