Conditions

Created: - Updated:

This article covers these topics:

Introduction to conditions

Conditions can be used before a question is shown (preconditions), or after a question has been answered (postconditions), to control which question is shown next, depending on the responses given to earlier questions.

Preconditions and postconditions

The preconditions and post-conditions are both defined in the same way in the XML, only with different element names:

<precondition if="..." then="..." else="..."/>
<postcondition if="..." then="..." else="..."/>

 

Attribute

Description

Default

if

The condition to be evaluated.

Mandatory attribute.

then

An action to take if the condition evaluates to true.

Optional attribute.
Default action for preconditions is to stop evaluation of preconditions and show the question.
Default action for post-conditions is to stop evaluation of post-conditions and go to next question.

else

An action to take if the condition evaluates to false.

Optional attribute.
Evaluation will continue to next condition if not specified and the condition evaluates to false.

You can add multiple conditions of each type to each question. The conditions are evaluated one at a time in the order specified in the XML until a condition is found that will stop the evaluation.

Operands

Operand

Description

answer(qref)

Returns the answer to the question qref

Note1: answers to multiple-choice questions are handled in a special way, as the answer variable can actually have multiple values. For example, if the respondent selected choices 2 and 5 in Q1, both the conditions answer(Q1)==2 and answer(Q1)==5 will evaluate to true.

Note2: the function answer() will return different results type depending on the question type. Because of this, we recommend the use of the more specific answer functions (answerid, answerlabel, answeralias) specified here below. 

answerid(qref)

Returns the id(s) of the selected choice(s) in the question qref

answerlabel(qref) Returns the label(s) of the selected choice(s) in the question qref
answeralias(qref) Returns the alias(es) of the selected choice(s) in the question qref

hasanswer(qref)

Check if an answer has been given to a single choice question or a multiple choice question

anscount(qref)

Answer count, i.e. for a multiple choice question the number of selected choices.

isset(var)

Check if specified variable has been set or not.

var(svar)

Returns the value of the survey variable svar

global(gvar)

Returns the value of the global variable gvar

random(min,max)

Generate a random number between the numbers specified in min and max, min and max included. For example if min is 1 and max is 10, the random number could be any value between 1 and 10.

getrespdata(sample_field_alias)

Returns the value of the sample field identified by sample_field_alias

setrespdata(sample_field_alias,value)

updates the value of the sample field identified by sample_field_alias

osname

Returns name of operating system. For iOS, returns “IOS”, for Android returns “ANDROID”. Pulled locally from the device.

osversion

Returns version number of operating system. Pulled locally from the device.

compareOSVersions(os_version1,os_version2)

Compares first three numbers in version number between two versions. Returns -1 if os_version 1 is earlier than os_version 2. Returns 1 if os_version1 is later than os_version2 Returns 0 if versions are the same.
getfencename() Retrieves the name (text string) of the geo fence that triggered the survey entry.
getfencealias() Retrieves the alias of the geo fence that triggered the survey entry.

If both operands return numeric values, they will be compared as numbers, otherwise as strings. If one of the operands is wrapped inside single quotations, they will also be compared as string. In string comparison, the larger than and less than operators will compare the string length.

Operators

It is possible to evaluate or combine operands with operators, summarised in the table below. Parentheses can be used to group statements together.

Operator

Description

= Equal to

!=

Not equal to

>

Greater than

>=

Greater than or equal to

<

Less than

<=

Less than or equal to

&&

Combine conditions using logical operator AND

||

Combine conditions using logical operator OR

!

NOT, used in front of operand, e.g. !hasanswer(Q3) will evaluate to true if there is no answer on question with alias Q3.

Question referencing

A questions can be referenced by either by its ID, prefixed with a # character, or by its alias. This applies to both questions and choices. For example:

answer(Q5a): shows the answer for the question with alias Q5a

answer(#12): shows the answer for the question with ID 12

Actions

When a condition is fulfilled (evaluated true), an action can be executed. For more information see Actions.