LiveCycle Designer customers often want to add simple calculations to a form, like calculating the sum of the values in two or more numeric field objects.
The following example shows how to write a script that calculates the sum of numeric field objects using FormCalc.
To perform calculations, FormCalc needs to know what objects in the form to include in the calculation. As a result, you need to name the Numeric Field objects that you want to sum on the Binding tab. Once you’ve named the numeric field objects, you can write a simple script using the field names, so that FormCalc knows exactly which fields to sum.
For example, if you have a numeric field object called grandTotal in which you want to display the sum of four other numeric field objects called total, stateTax, federalTax and shippingCharges, the FormCalc script you apply to the grandTotal numeric field object would look like this:
sum (total, stateTax, federalTax, shippingCharge)
Here is a summary of the steps involved:
- Add the numeric field objects to the form.
- Name each numeric field object in the Name box on the Binding tab.
- Select the numeric field object that you want to calculate the sum. For example, “grandTotal”
- In the Show list in the Script editor, select Calculate.
- In the Language list,select FormCalc .
- Add the “sum” script shown above to the Script Editor.
- Preview and test the form.
To learn more about writing simple scripts to make calculations, have a look at the tutorial called Creating a Purchase Order Form located here.
LiveCycle Designer also includes a sample form snippet called Calculate the Field Sum (CalculateSum.xdp), which demonstrates how to calculate sums of fields that are at different levels in the form hierarchy. Form Snippets are installed in the EN\Samples\Form Snippets folder under the Adobe LiveCycle Designer installation folder. The topic Calculating the field sums in the Designer Help (Creating Calculations and Scripts > Examples of Common Scripting Tasks) also demonstrates how to calculate the sums of fields located at different levels of the form hierarchy.