Calculated variables
Create derived variables by writing functions optionally using other variables
Last updated
Was this helpful?
Create derived variables by writing functions optionally using other variables
Last updated
Was this helpful?
Calculated variables allow you to define and calculate new answers based on other answers present within the flow, by writing simple functions that are evaluated within Formsort.
Common uses of calculated variables include:
Formatting answers
Capturing conditional logic that cannot be easily expressed
Performing math on numbers or dates
Calculated variables behave just like any other answer: they can be used for , into most text, and are sent to your analytics and integrations.
From the Variable tab, select Calculated variables, and click Add calculated variable...
Sets the expected that we are expecting the calculation to return: string
, number
, or boolean
If Is array? is enabled, then we will expect the response to contain multiple answers.
The Getter function body contains the Typescript code function body for the calculated answer.
For example, the following getter function body will result in the date 6 months from the current date:
To use existing answers within a getter function body, add the answers as variables. The following calculated variable will calculate the length of the answer variable labelled first_name
If any of the parameters to myFunction
are optional, such as in the case of API variables that may or may not be passed in, make sure to check the Optional field next to the variable.
If a variable that is not optional is not passed to the function, the Getter function will not execute correctly!
If a calculated variable has no answer dependencies, it is evaluated at the time that the flow loads.
For calculated variables with dependencies, by default, calculated variables are only evaluated once all of their required (non-optional) input variables are defined. If any of the required input variables become undefined, the calculated variable is cleared.
If only one or none of the input answers are defined, largest_number
will remain undefined.
Only when all of the input answers are defined will the calculation occur.
Enabling Blocks advancing from current step? in the configuration menu of your calculated variable will prevent the user from advancing past a step until calculations are finished processing. If no calculations have started yet (i.e. not all the dependencies are in), the step will not wait for the variable and the user will proceed. This ensures that calculated variables are present as the user proceeds through the flow, which helps to prevent logic errors down the line, and maintains data fidelity in the answers sent to your endpoint.
When using conditional evaluation, you must take care to handle undefined
answers within the getter function body.
Now that we're evaluating the calculation even when the inputs are undefined, we need to change the getter function body to handle this case.
If a calculated variable was calculated in a previous session, and the responder returns to the flow, the default behavior is not to re-calculate the answer.
To always recalculate the answer on load, enable Re-calculate on load. This is useful for situations when calculated variables are not idempotent, meaning that repeated invocations do not result in the same result, such as answers depending on the current date or time.
We can calculate a responder's age based on the date they enter for answer variable patient_dob
in this example.
Using this function, we can calculate the amount of days that have (or will) elapse from a date before or after today.
This function will return a boolean true/false value, based on whether or not the responder's answer to State
is included in a list.
This function requires a height
and weight
input, and will return a calculation of the users Body Mass Index.
For more examples on creating calculating variables, see at the bottom of this page.
Imagine the following calculated answer, which uses the javascript function to find the larger of two numbers, and we'll call largest_number
:
Calculated variables are handled asynchronously, and the default calculated variable configuration does not guarantee that the variable will be available by the time answer payloads are submitted. For instance, if a calculated variable finally receives the dependencies it needs on a step that will also send a payload (see ), the user might advance past the step before the calculated variable has time to finish it's calculations and attach the result to the payload, resulting in a seemingly missing calculated variable.
For more control over when the calculated variable is evaluated, you can set a using Is conditional?
Using the above example of largest_number
, we could add a condition using to run the calculation whenever either number is defined.