What do you need help with?

Guide to display conditions with code

10 min read

AI Doc Summarizer AI Summary

Display conditions let you display an object of the questionnaire according to specific conditions.

Objects that you can apply Display Conditions to are:

  • answer options
  • grid/matrix rows
  • grid/matrix columns
  • advanced grid/matrix cells
  • questions
  • groups of questions
  • pages
  • sections
  • custom alerts

Conditions can include values such as:

  • answer codes
  • values inserted in text answers (text, numbers, dates), rating and sliders.
  • contact fields

Logical operators #

Comparison symbols
These symbols can be used to compare two values.

  • == equal to
  • != different from
  • < less than
  • > greater than
  • <= less than or equal to
  • >= greater than or equal to

Negation
The negation symbol reverses the Boolean result of the condition. It must be placed before the condition.

  • ! negation

Refer to data #

In IdSurvey Display Conditions you can refer to different types of data.

Answer code
By writing the question code as the first value, IdSurvey will compare it with the answer code written after the operator.

if( Q1 != 1 )
Checks if answer code 1 was not selected on question Q1.

Values from open end answers, grids, rating and slider
By writing question code between square brackets, IdSurvey will compare values inserted in open end questions, rating and slider.

if( [Q1] <= 50 )
Checks if the value inserted as answer in question Q1 is less or equal to 50.

if( [Q2.1] == 80 )
Checks if the value inserted in the row 1 of grid question Q2 is equal to 80.

if( [Q3.2.1] != 10 )
Checks if the value inserted in the cell made by crossing of row 2 with column 1 of 3D Matrix Question Q3 is different than 10.

Values drag&drop
By writing question code followed by the answer code of a drag&drop question, IdSurvey will compare the position of that answer in the list.

if( Q1.1 == 1 )
Checks if answer .1 of drag&drop question Q1 is placed as first in the list.

if( Q1.3 != 1 )
Checks if answer .3 of drag&drop question Q1 is placed in a position different than the first in the list.

Values from contacts database fields
To refer to a value of the contacts database, you have to write the name of the field between curly brackets.

if( {gender} == m )
Checks if the field “Gender” of each respondent is populated with the value “m”

Note

If you use IdCodeidcode icon: the condition must start with /F and the condition must be enclosed in quotation marks. Example /F “if(Q1 != 1)”

If you use the Code Editorif icon: you can write the condition directly, without /F and without quotation marks. Example Q1 != 1

Notes

  • You can refer to a value inserted in (square brackets) only with open end questions, rating and slider. For this types of question IdSurvey records not only the answer code but also the value inserted by the respondent.
  • Only a single value for each simple question, one value for each row of a grid and one value for each cell of an advanced grid can exist. For more information check IdSurvey guide or the specific article of the Knowledge base.

Conventions #

For convention, all conditions based on questions not shown give back a false result.
For example, if question Q15 was not shown because it was filtered, and the condition if( Q15 != 3 ) was applied to question Q20, question Q20 won’t be shown even if the answer is different than 3.
This convention ensures a more natural behaviour for the way the display conditions are generally conceived in a questionnaire.

You can avoid this convention using the operator not (negation). For example, if you apply the condition if( !Q15 == 3 ) to question Q20, this question will be shown even if question Q15 was not displayed. Show if at question Q15 was not selected answer option 3.

Display condition with “And” (&&) #

Display conditions with “And” symbol display an element of the questionnaire when all conditions with this symbol are simultaneously true. 
The symbol for conditions with “And” is: &&

For example:

Let’s set the following display condition in question Q4

if(Q1==2&&Q2==2&&Q3==1)

In this case question Q4 is displayed only if I selected answer with code “2” to question Q2 and answer “2” at question Q1 and answer “1” at question Q3.
If even one of those condition is not true, question Q4 will not be displayed.
So if in question Q3 I select answer “3”, question Q4 will not be displayed.

Display condition with “Or” ( || ) #

Display conditions with “Or”  symbol display an element of the questionnaire when at least one of the conditions with this symbol is true.
The symbol for conditions with is: ||

For example:

Let’s set the following display condition in question Q6

if(Q4==2||Q5==2)

In this case question Q6 is displayed if I select answer with code “2” to question Q4 or answer “2” to question Q5.
You need just one of the conditions specified above to be true for question Q6 to be displayed.

Use both “OR” and “AND” operators in the same condition #

You can combine multiple conditions of any level of complexity, also combining both && and || operators.

For example:

Let’s set a hypothetical question Q3 with the following display condition

if( Q1==99 || ( Q1==1 && Q1==2 ) )

In this case question Q3 will only be displayed if answer with code “99” was selected at question Q1 or if both the answers “1” and “2” were selected on question Q1.

Please note that the condition Q1==1 && Q1==2 is in brackets. This is to indicate the order of elaboration. In this specific example the result of the condition will therefore be elaborated with the condition outside the brackets, so that is Q1==99 || [result of the bracketed condition]

Note:

  • Without brackets, IdSurvey will process AND conditions before OR conditions. Even if we would suggest always using brackets in order to avoid ambiguities of the order of elaboration, the condition of the example above could also be written without brackets.

Algebraic calculation #

You can formulate algebraic expressions of any complexity within the conditions using the mathematical operators sum, subtraction, multiplication, and division (+, -, *, /).

if( ([Q1]+[Q2]) <= 100 )
it checks if the sum of the numerical answers given to question Q1 and Q2 is less than or equal to 100.

if( [Q5] == (retrieve(Q1, open)/2) )
it checks if the number inserted in the open-end option of question Q5 is equal to half of that entered in question Q1.

Functions #

IdSurvey provides a number of functions that can be used in display conditions, in FlowScript and in IdCode. The functions allow you to perform checks on questions, contact fields or quota status. For example, check how many answers have been selected to a question with multiple answer options or check if a certain quota is closed, etc…
Other special functions allow you to process strings, retrieve the label of an option of a drag&drop question by position (ranking) and much more.

CountCodeIt allows you to count the number of options selected.if(CountCode (Q1) >= 3)
It checks if on question Q3 three or more options were selected.
ResponsesCodeOfIt gives back the codes of the answers selected in a question.if(Q2==responsesCodeOf(Q1)
It checks if the answer selected at question Q2 has the same code of that selected at question Q1.
CountResponsesWithCodeIt allows you to count the number of questions to which a specific response code was answered.if(CountResponsesWithCode(Q1,Q2,Q3,Q4,Q5,Q6,99) >= 3)
It checks if code 99 was answered to a minimum of three questions among Q1, Q2, Q3, Q4, Q5 and Q6.
CountResponsesWithTextIt allows you to count the number of questions to which a specific text or number was answered.if(CountResponsesWithText (Q1,Q2,Q3,Q4,Q5,Q6,'sea') >= 3)
It checks if the word “sea” was entered on at least 3 of the text answers on questions Q1, Q2, Q3, Q4, Q5 and Q6.
CheckQuotaIt allows you to verify if a certain quota is open or full.if(CheckQuota ('Parents') == 1)
It checks if the quota “Parents” is full.
CompareDateIt compare two dates.
It gives -1 if the first date precedes the second one, 0 is they are the same and 1 if the first date is subsequent to the second one.
if(compareDate({date_of_birth}, ‘2010-10-01') == -1)
It checks if the date in the “date_of_birth” field precedes the date specified.
ModuleIt gives back the string CATI, CAWI or CAPI according to the module with which the interview is currently being answered.if(module()==cawi)
It checks if the interview is currently being answered via CAWI.
ContainsTextIt allows you to verify the presence of the specific text within an open-ended answer.if(ContainsText ([Q1],'sea')==1)
It checks if the text answer to question Q1 contains the word “sea”.
StartWithTextIt compares the beginning of a string with a specified string.if(StartWithText({Mobile},'+39')==1)
It checks if the field Mobile starts with “+39”.
EndWithTextIt compares the end of a string with a specified string.if(EndWithText({Identification},'123')==1)
It checks if the field Identification ends with “123”.
GetVariableIt allows to obtain the value of a variable (on the contact or on a question) dynamically creating the name of the variable itself.getVariable('{date_of_birth_son_'+{ison}+'}')
It obtains the value of the “ison” variable (for example 3) and uses it to generate the name of the variable “date_of_birth_son_3” getting the value related.
JoinIt allows to force the combination of strings preventing that these are interpreted as numbers. For example 10+02+12 goes 24, while join(10,02,12) gives 100212.join(10,02,12)
It creates the string 100212.

join(text,02,[Q1])

It creates the string text02openResponseQ1.
RandomIt creates a random number between the minimum number and the maximum number indicated. You can specify a name to save the number generated in order to use it afterwards.random(1, 10)
It creates a random number between 1 and 10.
random(1, 10, 'my random number')
It creates a random number between 1 and 10 and saves it with the name specified. Using the same syntax (in the FlowScript of any other page of the questionnaire) the number saved and generated the first time will be called again. 
ReplaceIt replaces the specified text within a string.(Available starting from IdSurvey 8.1)Replace([Q1], 'car', 'bus')
It gives back the string of the text answer of question Q1 replacing the word “car” with “bus”.

Replace({phone}, '+39', '')

It gives back the field phone without the prefix +39.
GetItemByRankingIt gets back the label or the code of the item according to its position.(Available starting from IdSurvey 8.1)GetItemByRanking(Q1, 1, label)
It gives back the label of the element in first position in the drag&drop question (or – for the matrix – it gives back the text of the row where the answer with code 1 is selected).

GetItemByRanking(Q15, 3, code)

It gives back the code of the element placed in third position in the drag&drop question (or – for the matrix – it gives back the text of the row where the answer with code 3 is selected).

GetItemByRanking(Q1, 1, auto)

or
GetItemByRanking(Q1, 1)

It gives back the label of the element in first position in the drag&drop question. If the label is not available you get the code.
RetrieveIt returns the answer selected and allows you to specify the data to be recovered and the formatting of the multiple answer separator.(Available starting from IdSurvey 8.1)retrieve(Q1)
or
retrieve(Q1, auto)
It gives back the open end answer (if available) or the label of the answer selected in question Q1. In case of multiple answers, you get all the options separated by the pipe character “ | ”.

retrieve(Q1, auto, ', ')

As the previous example but in case of multiple answers, you get all options separated by a comma and a space.

retrieve(Q1, opentext)

It gives back the text answer of question Q1.

retrieve(Q1, label)

It gives back the label of the answer selected in question Q1.

retrieve(Q1, code)

It gives back the code of the answer selected in question Q1.

retrieve(Q1, commentbox)

It gives back the text inserted in the comment box.
FindContactId and CurrentReturns the id of the first contact that matches a search condition in the current survey or, if specified, in the indicated survey. If no match is found, it returns an empty value. In the search condition, fields are evaluated in the context of the contact found by the function; to compare them with the values of the current contact, you must use current(field).if(findcontactid(({Email}=='john.smith@example.com'))!='')
Checks whether at least one contact with email equal to john.smith@example.com exists in the current survey.
if(findcontactid(({Email}=='john.smith@example.com'), 3050)=='')
Checks whether no contact with email equal to john.smith@example.com exists in survey 3050.
if(findcontactid(({Email}==current({Email})), 3050)!='')
Checks whether at least one contact with email equal to the current contact’s email exists in survey 3050.
What are your feelings