How to use Global Variables

Overview

We have 3 different options when using the Global Vars:

1. GlobalVars[] - Holds any information, according to the logic you implement, on the device level which means you can use it at any of the interviews running on the device no matter what survey they are from. 

2. SurveyGlobalVars[] -  Holds any information, according to the logic you implement, on the specific survey level that it was defined for and use it across interviews that are of that specific survey on that device. 

3. Vars[] - Hold data in a global variable which will be available to fetch from every single place in your survey – Global variables are the answer.
Global Variables are used to store different data, making it available through all of your survey.
This how to will instruct on how to create global variables and how to use them.

How Global Variables Are Constructed?

Global variable need to have to parameters:
1. The Key (the name of the variable).
2.The Value (Can be a number, a text input or a Boolean value)

For Example:

Vars["KEY"]=VALUE;

‘Vars’ is basically declaring that this is a global variable.

Please note: where in the example you see 'Vars' you can also use either 'GlobalVars' or 'SurveyGlobalVars'

Where To Create A Global Variable?

In any place you want across your survey (start & end scripts\advanced scripts\ survey init code) you can declare

How To Create A Global Variable?

Here are real examples of how you can create global variables:


Vars["PointLimit"]=100;

Vars["TheWinningName"]=“Jessica”;

Vars["toStopSurvey"]=false;

Vars["test1"]=“a”;

Vars["test2"]=7;

As you can see, we have declared the ‘Vars’ component in order to make the system ‘know’ it is global.
We used the square bracelets, and placed the key name within.

Then we set a value to our keys.

How To Access A Global Variable?

In any place in your survey which you need to have any use of one of your global variables, you can just write it with its name, for example:

Vars["PointLimit"]

Vars["TheWinningName"]

Vars["toStopSurvey"]

Vars["test1"]

Vars["test2"]

Like this you can use the value they hold in rules,scripts,etc’.

For Example:

if(Answer(CurrQues)>=Vars["PointLimit"])

{

    // DO SOME NICE CODE

}

If you wish to change a global variable value, just add the ‘=’ operator and write the new value, for example:

if(Answer(CurrQues)>=Vars["PointLimit"])

{

    Vars["PointLimit"])= Answer(CurrQues);

}

That’s it!

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

1 comment
  • Hi,

    Refer to the iteration logic I got the other time that led to defining global variables, while the logic work well I think it can be improved upon. For instance, if "it is the only brand I drink" is chosen in iteration index 1, that code, i.e. code 1 will not appear in other iteration indexes, which is good anyway. The key issue here is that assuming you have reached iteration index 5 and you want to go back to previous iteration indexes, you will not find that code 1 available in iteration index 1 and consequently, iteration index 1 will force you to choose another code and when you choose another code for iteration index 1, what you coded earlier will not be visible in any of the iteration indexes again. So this makes it difficult to edit the whole iterations. Any solution to this?

    Regards,

    Johnson

Please sign in to leave a comment.