How to Use Custom Variables in Form Calculations
About Custom Variables
Custom variables act as intermediate or auxiliary variables used in form calculations. They enable you to perform various calculations in the background and access the resulting expression value using a unique custom variable name (ID). This value can then be used in conditional logic to control the behavior and visibility of other questions within the form.
A common use case for a custom variable is to calculate a respondent's age based on the date entered in a "Date of birth" field and then show or hide certain questions depending on the age value. Alternatively, you can use the custom variable in a survey completion trigger that will direct the respondent to the "Thank You" page if their calculated age falls below a specified threshold.
How to Create a Visibility Rule Based on a Custom Variable Value
The steps below describe how to create a custom variable using the age()
function and conditionally show a follow-up question depending on the calculated age value.
- In the Toolbox area, hover over Single-Line Input and select Date from the list of available input types.
- Enter a question ID in the Question name text box.
- Enter a user-friendly value in the Question title text box. This value will be visible to respondents.
- Switch to the survey-level settings in the top right corner.
- Under Conditions, locate the Custom variables subsection.
- Click the Plus icon to create a new variable.
- Enter a unique Name for the variable.
- Enter the following sample value in the Expression field:
age({date-of-birth})
, wheredate-of-birth
is the "Question name" value of the field in which a respondent enters their date of birth.
- Add a dependent question to your form. In this example we are using a Yes/No (Boolean) question.
- Enter a question ID in the Question name text box.
- Enter a user-friendly value in the Question title text box.
- Under Condition, locate the Make the question visible if property.
- Click the Magic wand icon on the right of the property to open a pop-up dialog.
- In the dialog, select the custom variable name assigned in step 7 and the condition that has to be met in order for the dependent question to be visible.
- Enter the desired threshold value and click Apply.
Based on the configuration above, the "Do you have a driving license?" question will only be visible if a respondent's age is greater than or equals to 16.
To test the custom variable configuration, switch to the Preview tab and select or enter a value in the Date of birth question.
How to Complete a Survey Based on a Custom Variable Value
The steps below describe how to create a trigger that will prompt a respondent to the "Thank you" page based on the age value calculated using a custom variable.
- Complete steps 1-8 from the previous section of the guide.
- Switch to the survey-level settings in the top right corner.
- Under Conditions, locate the Triggers subsection.
- Click the Plus icon to create a new trigger.
- Select Complete survey from the drop-down menu on the left.
- Click the Magic wand icon on the right of the Expression property to open a pop-up dialog.
- In the dialog, select the custom variable name and the condition that has to be met to trigger survey completion.
- Enter the desired threshold value and click Apply.
Based on the configuration above, the survey completes automatically if a respondent's age is less than 16.