Using Conditional Logic in Form Calculations
  • 17 May 2024
  • 2 minute read
  • Dark
    Light
  • PDF

Using Conditional Logic in Form Calculations

  • Dark
    Light
  • PDF

Article summary

Slate forms offer the option of calculating the value of a field on the fly, based on other items on the form. This allows options such as calculating a fee to be paid and varying that fee based on selections by the user, or calculating a test score summary based on scores that are entered. If you are hoping to use form calculation for values other than to calculate an amount due, please check out this Knowledge Base Article on Form Conditional Logic.

Until recently, these calculations often required a snippet of code. For example, if you wanted to charge a different deposit based on whether an applicant plans to live on-campus or off-campus, you had two options. One was to make the payment amount be the value of the form field that they choose (by making the option something like "Plan to live on campus^600"), and the other was by writing a complex bit of code such as "(@location == 'Plan to live on campus' ? 600 : 300)".

Both of these options will still work, but we've introduced a third option that you may find simpler. You can now add multiple hidden fields for the different possible values, and then use the built-in conditional logic to activate one of those values.

Examples

First, add two or more text fields that will contain the possible values for your calculation. All of the fields should use the same export key. The fields can be marked as hidden, and if the values are numeric, the Data Type should be set to Integer or to Real.

Next, add a default value in the desired amount, and mark the field as Hidden (accessible through script):

1.png

2.png

Finally, add conditional logic filters to this field indicating when it should be active:

3.png

This approach can be used for a payment widget calculation or any form field calculation. It can be used when selecting between two values or many values. Because this uses our standard conditional logic, it should be possible for any Slate user to set up calculated fields without the need to write code.

Technical Details

This new option works because of a couple of small changes. It's always been the case that if you have a hidden field, and you add conditional logic to that field, the logic will cause it to change from active to inactive (rather than visible to invisible). So, when using a placeholder such as "@deposit" to find a value in a calculation, Slate will now check to see if multiple matches exist, and if they do, it will select the value that is active.

Second, whenever a field changes from active to inactive or vice versa due to conditional logic, any calculation scripts on the form that are looking at the value of that field will be triggered. Previously, calculation scripts were triggered when the value itself changed, but not when the value became active or inactive (or visible or invisible). Now they will be.


Was this article helpful?