Forms: Autofill Fields

In DSPTCH forms, you can automatically populate fields using expressions and built-in functions. These expressions allow you to reference other answers, perform calculations, and dynamically generate values in real time as the user completes the form. For example, you can combine multiple fields into a single value, evaluate conditions, or derive outputs using functions like iif , sum , or age . This guide provides examples and explains how to use expressions to auto-fill fields within your forms.

Expressions require compatible data types to function correctly. For example, when using sum()  to combine values from multiple responses, both the input fields and the target field must use a numeric data type.

To see all possible expressions, view the documentation below:

In the documentation, when you see a value in brackets {}  , that refers to the Question name.


For example, if you wanted to sum the values in question1   and question2 , in Default value expression you would put sum({question1}, {question2})  




iif   

Definition: iif(condition: expression, valueIfTrue: any, valueIfFalse: any): any   

Returns the valueIfTrue    value if the condition    is truthy or the valueIfFalse    value if the condition    is falsy.

Example: "expression": "iif({question1} + {question2} > 20, 'High', 'Low')"   


isContainerReady   

Definition: isContainerReady(nameOfPanelOrPage: string): boolean   

Returns true    if all questions in a given panel or page have valid input; otherwise, returns false   . An empty question value is considered valid if neither validators nor required status is defined for it.

Example: "expression": "isContainerReady('page1')"   


isDisplayMode   

Definition: isDisplayMode(): boolean   

Returns true    if the survey is in display or preview mode.

Example: "expression": "isDisplayMode()"   


age   

Definition: age(birthdate: any): number   

Returns age according to a given birthdate. The date argument (which is typically taken from a question) should be defined as a valid JavaScript Date.

Example: "expression": "age({birthdate})"   


currentDate   

Definition: currentDate(): Date   

Returns the current date and time.

Example: "expression": "currentDate()"   


today   

Definition: today(daysToAdd?: number): Date   

Returns the current date or a date shifted from the current by a given number of days. For example, today()    returns the current date, 0 hours, 0 minutes, 0 seconds; today(-1)    returns yesterday's date, same time; today(1)    returns tomorrow's date, same time.

Examples:

  • "expression": "today()"   
  • "expression": "today(2)"   

year   

Definition: year(date?: Date): number   

Returns the year of a given date.

Example: "expression": "year({birthdate})"   


month   

Definition: month(date?: Date): number   

Returns the month of a given date as a value from 1 (January) to 12 (December).

Example: "expression": "month({birthdate})"   


day   

Definition: day(date?: Date): number   

Returns the day of the month for a given date as a value from 1 to 31.

Example: "expression": "day({birthdate})"   


weekday   

Definition: weekday(date?: Date): number   

Returns the day of the week for a given date as a value from 0 (Sunday) to 6 (Saturday).

Example: "expression": "weekday({birthdate})"   


getDate   

Definition: getDate(question: expression): Date   

Returns a Date value converted from a given question's value.

Example: "expression": "getDate({birthdate})"   


dateAdd   

Definition: dateAdd(date: any, numberToAdd: number, interval: "days" | "hours" | "minutes" | "seconds" | "months" | "years"): Date   

Adds or subtracts a specified number of days (default), hours, minutes, seconds, months, or years to or from a date value.

Example: "expression": "dateAdd({startDate}, 14, "days")"   


dateDiff   

Definition: dateDiff(fromDate: any, toDate: any, interval: "days" | "hours" | "minutes" | "seconds" | "months" | "years"): number   

Returns a difference between two given dates in days (default), hours, minutes, seconds, months, or years.

Example: "expression": "dateDiff({birthdate}, today(), "months")"   


sum   

Definition: sum(param1: number, param2: number, ...): number   

Returns the sum of passed numbers.

Example: "expression": "sum({total1}, {total2})"   


max   

Definition: max(param1: number, param2: number, ...): number   

Returns the maximum of passed numbers.

Example: "expression": "max({total1}, {total2})"   


min   

Definition: min(param1: number, param2: number, ...): number   

Returns the minimum of passed numbers.

Example: "expression": "min({total1}, {total2})"   


avg   

Definition: avg(param1: number, param2: number, ...): number   

Returns the average of passed numbers.

Example: "expression": "avg({total1}, {total2}, {total3})"   


round   

Definition: round(num: number, precision?: number): number   

Rounds the given number to the specified number of decimal places. If the precision    parameter is omitted, the given number is rounded to the nearest integer.

Examples:

"expression": "round({numericQuestion}, 2)" // Rounds the value of `numericQuestion` to two decimal places
"expression": "round(0.5)" // 1
"expression": "round(-0.5)" // -1
"expression": "round(1.005, 2)" // 1.01
"expression": "round(2.175, 2)" // 2.18
"expression": "round(-1.005, 2)" // -1.01
"expression": "round(-2.175, 2)" // -2.18

trunc   

Definition: trunc(num: number, precision?: number): number   

Truncates the given number to the specified number of decimal places. If the precision    parameter is omitted, only the integer part is returned. Trailing zeroes in the decimal part are removed after truncation.

Examples:

"expression": "trunc({numericQuestion}, 2)" // Truncates the value of `numericQuestion` to two decimal places
"expression": "trunc(0.5)" // 0
"expression": "trunc(-0.5)" // 0
"expression": "trunc(1.005, 2)" // 1
"expression": "trunc(2.175, 1)" // 2.1
"expression": "trunc(-1.005, 2)" // -1
"expression": "trunc(-2.175, 1)" // -2.1

sumInArray   

Definition: sumInArray(question: expression, dataFieldName: string, filter?: expression): number   

Returns the sum of numbers taken from a specified data field. This data field is searched in an array that contains a user response to a Multi-Select Matrix, Dynamic Matrix, or Dynamic Panel question. The optional filter    parameter defines a rule according to which values are included in the calculation.

The following code sums up values from a "total"    matrix column but includes only the rows where a "categoryId"    column equals 1:

Example: "expression": "sumInArray({matrixdynamic}, 'total', {categoryId} = 1)"   


maxInArray   

Definition: maxInArray(question: expression, dataFieldName: string, filter?: expression): number   

Returns the maximum of numbers taken from a specified data field. This data field is searched in an array that contains a user response to a Multi-Select Matrix, Dynamic Matrix, or Dynamic Panel question. The optional filter    parameter defines a rule according to which values are included in the calculation.

The following code finds a maximum value within a "quantity"    matrix column, but the value should be under 100:

Example: "expression": "maxInArray({matrixdynamic}, 'quantity', {quantity} < 100)"   


minInArray   

Definition: minInArray(question: expression, dataFieldName: string, filter?: expression): number   

Returns the minimum of numbers taken from a specified data field. This data field is searched in an array that contains a user response to a Multi-Select Matrix, Dynamic Matrix, or Dynamic Panel question. The optional filter    parameter defines a rule according to which values are included in the calculation.

The following code finds a minimum value within a "quantity"    matrix column but searches for it only in the rows where a "categoryId"    column equals 1 and the values are positive:

Example: "expression": "minInArray({matrixdynamic}, 'quantity', {quantity} > 0 and {categoryId} = 1)"   


avgInArray   

Definition: avgInArray(question: expression, dataFieldName: string, filter?: expression): number   

Returns the average of numbers taken from a specified data field. This data field is searched in an array that contains a user response to a Multi-Select Matrix, Dynamic Matrix, or Dynamic Panel question. The optional filter    parameter defines a rule according to which values are included in the calculation.

The following code finds an average of values within a "quantity"    matrix column, excluding zeroes:

Example: "expression": "avgInArray({matrixdynamic}, 'quantity', {quantity} > 0)"   


countInArray   

Definition: countInArray(question: expression, dataFieldName: string, filter?: expression): number   

Returns the total number of array items in which a specified data field has a value other than null    or undefined   . This data field is searched in an array that contains a user response to a Multi-Select Matrix, Dynamic Matrix, or Dynamic Panel question.

The following code finds the total number of matrix rows with a "quantity"    column value greater than zero but includes only the rows where a "categoryId"    column equals 1.:

Example: "expression": "countInArray({matrixdynamic}, 'quantity', {quantity} > 0 and {categoryId} = 1)"   


displayValue   

Definition: displayValue(questionName: string, value?: any): any   

Returns a question's display text. Supports questions nested within panels or matrices.

The second parameter allows you to get a display text associated with a specific value rather than with the current question value. For instance, the following expression references a display text that corresponds to value 5 in a Dropdown question. If you don't pass the second parameter, the displayValue    function returns a display text for the currently selected question value.

Example: "expression": "displayValue('my-dropdown-question', 5)"   

When using the displayValue    function within a setValueExpression   , specify the setValueIf    expression as well. This requirement stems from the fact that the setValueExpression    is reevaluated only when setValueIf    is true    or once the value of a referenced question is changed. Although you do pass a question name to the displayValue    function, this name is only used to access the question within JavaScript code and doesn't directly reference it. To trigger the reevaluation in this case, define the setValueIf    expression as follows:

"setValueExpression": "displayValue('my-dropdown-question')",
"setValueIf": "{my-dropdown-question} notempty"

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us