Building & Using Calculations
Building Calculations Calculations must conform to these rules exactly or the form will generate Javascript Errors. You will know there is a problem with your calculations when there is a yellow Explanation icon in the lower left corner of the browser window. Start with a simple calculation and build on it, testing it often. If you get stuck, please contact a support representative and they will review your calculation and help you through this process. Overview Calculations are attached to calculation fields. These fields will typically display a value to the user, although they are also permitted as hidden fields. Calculations are executed from the top down, please bear this in mind when laying out your form. A calculation can be as simple as adding two fields on the form and displaying the total or as advanced as an IF/THEN/ELSE statement that intelligently determines the calculation value. Basic Calculation Statements (No IF/THEN/ELSE) An example of a basic statement to set the value of a calculation field would be: [subtotal]=[subtotal]+300or to completely replace the value in the subtotal field: [subtotal]=300 Where SUBTOTAL is the name of the calculation field. This simple expression would ADD 300 to the current value of the SUBTOTAL field. Another example, which calculates the total of 2 fields and displays the result as the TOTAL might look like this: [subtotal]=[subtotal]+[Shipping]+[tax] When writing these statements you can use parenthesis to breakdown the equation: [subtotal]=[subtotal]+(([Shipping]+[tax])-[Discount]) IF/THEN/ELSE Statements The real power of the calculation fields comes from the ability to create IF/THEN/ELSE statements. These work the way they sound. They first check a form field values and if it matches a particular value, then the specified calculation is executed. Here is an example: Example of an IF/THEN statement: IF [STATE] = 'California' THEN [tax]=[subtotal]*.07 ENDIF IF [STATE] = 'California' THEN [tax]=[subtotal]*.07 ELSE [tax]=0 ENDIF **When using the ELSE STATEMENT, the next two rows must contain a calculation and then the ENDIF statement. Example of an IF/THEN/ELSEIF statement: In this example, multiple statements are evaluated. The statement is executed from the top down. When the first "IF" statement does not match, the next "ELSEIF" clause if run. If it matches, the calculation following the "THEN" keyword is executed. If there is no match, the next "ELSEIF" is run and so on. This repeats to the end of the statement. If there are no matches, and the statement includes a "ELSE" keyword at the bottom, the final calculation (the default) will execute.
IF [STATE] = 'California' THEN
[tax]=[subtotal]*.07
ELSEIF [STATE] = 'Washington' THEN
[tax]=[subtotal]*.05
ELSE
[tax]=0
ENDIF
The IF/THEN/ELSE statement can be continued in this fashion to support an unlimited number of ELSEIF statements. You can also make the statements being evaluated more complex by using the two operators "AND/OR" while using parentheses. Here is an example of
IF ([STATE] = 'California' OR [STATE] = 'OHIO') AND [CHARGETAX]='TRUE' THEN
[tax]=[subtotal]*.07
ENDIF
In the above example (the tax calculations are fictional and inaccurate!) the STATE field is checked to see if it equals either "California" or "Ohio". Because of the parenthesis, this statement is executed first. Then, the AND operator is used to check and see if a flag labeled "CHARGETAX" equals TRUE. If it does, then the tax is calculated. NESTED IF STATEMENT There is currently no support for nested if statements. MULTIPLE IF/THEN/ELSE STATEMENTS PER CALCUALTION FIELD You can include multiple IF/THEN/ELSE statements within a calculation field. To do this, simply leave a line feed at the end of the first statement and start the next one. Here is an example:
IF [STATE] = 'California' THEN
[tax]=[subtotal]*.07
ELSEIF [STATE] = 'Washington' THEN
[tax]=[subtotal]*.05
ELSE
[tax]=0
ENDIF
IF [SUBSCRIPTION] = '12 months' THEN
[subtotal]=[subtotal]+1200
ELSEIF [SUBSCRIPTION] = '6 months' THEN
[subtotal]=[subtotal]+700
ENDIF
The following operators are available when building calculations: Criteria Operators: =,<>,>,< Boolean Operators: AND OR Math Operators: +,-,*,/ Conclusion Creating complex calculations is fairly easy using this limited subset of keywords and IF/THEN/ELSE statements. However, the syntax of the statements is not validated and it can be easy to forget a word, or forget a statement. Be sure to test your calculations often and always start with a simple statement and build on it. If when you return to your form, it generates javascript errors, you should carefully review the calculation for errors. If you can not find the error, send the name of the form and the calculation field to support and a representative will help you to complete the calculations. |