How do I sum / add values across multiple instances of a subform? (Advanced)

sumSubform($subform$, 'fieldkey')

If you are wanting to add/sum a fieldkey or fieldkeys across multiple instances of a subform, then this article will show you how to do that. I'll be going over the basics of how to accomplish this. If you need more information on scripting and skip logic, then you can find more general info here Scripting and Skip Logic

This article assumes you have a general understanding of how to use and calculate using our calculation field type. Also if you aren't sure about what a subform is and how to use it, you can view this video on Subforms

This feature takes an advanced understanding of how subforms and scripting works. Though once it has been accomplished, it is relatively simple to setup in your forms moving forward.

Make sure that you have already created a main form and subform and that you have already added a subform field type to your main form

Subform Setup

In my example, I have an Order form, which is my main form. I also have a subform already setup (pictured below) with a Product list (also shown below) that has produce (apples, oranges, watermelon etc) with scores (in the red box) associated with those selections.There is also a Quantity field, and a Sub Total field.

For my Sub Total (calculation field) I want to multiply the score of my list selection by the quantity to get my subtotal. This is how I set up the calculation. I dragged over from left to right the Product list and Quantity fields. I added a multiplication symbol * between the two field keys and then put a # symbol in front of the Product list field key. I did this to let the code know to calculate the score (red box above) of the list selections instead of the value.

You can use almost any operator in the calculation field to get your desired results. You can do averages, multiplication, percentages, etc. You can also use if/then statements and variables to get your desired results.

After you have setup your desired calculation, I would recommend saving and publishing your subform and testing on your device to make sure that the calculation displays and computes how you want it. It often takes many tests to get your desired results depending on how complex your calculation is.

2. Add a calculation field in your main form. This calculation field will be used in conjunction with a formula to pull the values across multiple instances of your subform and total them in the calculation field

3. Add the function sumSubform($subform$, 'fieldkey') into the calculation field in your main form.

4. Replace the $subform$ with your subform picker's field key (image 1). Make sure to keep the dollar signs $ on the outside of your fieldkey (image 2).

IMAGE 1

IMAGE 2

5. Replace the 'fieldkey' with the field that you want to add / sum across your subform instances (image 2). It is important to use single quotes for this part instead of using dollar signs.

Once you have successfully added both of those field keys to your function, then save and publish your form.