In this article
Note: Forsta uses the Syntax Highlighter functionality. The syntax highlighter is enabled by default in all areas where scripts can be written. To disable the syntax highlighting, check “Disable script highlighting” in the User Settings overlay. When the syntax highlighter is in use, each time the Tab key is pressed, the line of script is indented one step. Each press of the Shift-Tab keys removes one indent from the line. Note that the Tab key does not insert a tab in the text. Refer to the Professional Authoring documentation for further details.
The first example incorporates the use of Global scripts and Action scripts.
For Global script, the defined script will be executed once before the action script is started. In this case the Global script initializes a Customer Code Library, and this needs to be done before the script can retrieve information.
Note: If you want to perform action scripting based on interview_start, you must add the interview_start variable.
Cross-referencing city name and zip-code
The Customer Code library used in this example contains the details of which zipcode belongs to which city. The cross-references between city names and their appropriate zip codes are stored in a look-up table (Hashtable), which must be declared as a global variable.
The actual script can use this information as a look-up table, so in the survey, when for example a question only asks for the zipcode, the City name can automatically be retrieved.
The following code must be added to the Global Variables tab:
var zipCode2CityMap : Hashtable;And to the Global Script tab could be added:
zipCode2CityMap = MyCustomCodeLib.GetZipCode2CityMapping();To the Script tab add:
var zipCode = ZipCodeVariable;
var city = zipcode2CityMap[zipCode];
CityVariable = city;
Calculating numeric age and age-band
This script takes the respondent's date of birth (in this case it has been entered as three separate questions – day, month and year) and calculates a numeric age and age-band.
if(!IsNull("Year_1"))
{
var year = Year_1;
var month = Month_1-1;
var day = Day_1;
var birthday= new Date(year,month,day);
var today = new Date();
var years = today.getFullYear()-birthday.getFullYear();
birthday.setYear(today.getFullYear());
// If your birthday hasn't occurred yet this year, subtract 1.
if(today.valueOf() < birthday.valueOf())
{
years=years-1;
}
agenum = years;
if (agenum>=16 && agenum<=24)
{
Agebands="1";
}
if(agenum>=25 && agenum<=34)
{
Agebands="2";
}
if(agenum>=35 && agenum<=44)
{
Agebands="3";
}
if(agenum>=45 && agenum<=54)
{
Agebands="4";
}
if(agenum>=55 && agenum<=64)
{
Agebands="5";
}
if(agenum>=65)
{
Agebands="6";
}
}Importing data from single questions into a grid
This scenario might be important when data is imported from a data source that has no concept of Grids, but we want to import the data to a grid in Forsta Plus so it can be used for example in Reportal.
The action script may be followed by some explanations in the comments (// in front of the text). In this scenario, the external data source has three single questions (Comfort, Price and Color) which are to be imported into a grid (Qualities) containing three answers (Comfort, Price and, Color) in the Forsta Plus survey.
// The external data source contains 3 single questions: Comfort, Price and Color
var singles = [‘Comfort’,’Price’,’Color’];
// The grid “Qualities” in the Forsta Plus survey contains 3 answers with the following codes: Comfort, Price, Color
var gridCodes = [‘Comfort’,’Price’,’Color’];
// A loop iterates 3 times in this case since the array contains 3 questions
for (var i = 0; i < singles.length; i++)
{
// if the value of the current respondent of the individual single variable is null the individual grid variable will be set equally to null
//if the value of the current respondent of individual single variable is not null , the value of the single variable will be set to the individual grid variable
var singleVariable = singles[i];
var gridVariable = ‘g1_’ + gridCodes[i];
if (IsNull (singleVariable))
SetNull(gridVariable);
else
SetValue (gridVariable, GetValue(singleVariable));
}
Setting a group of variables to NULL
If you want to set the value of all respondents of a group of variables to NULL, you can do this with the following script:
// Generate an array with the questions you would like to set to NULL
var variables = [‘q1’,’q2’,’q3’,’q4’];
// loop iterates through all questions from the array
for (var i = 0; i < variables.length; i++)
{
// variables will be set to Null
SetNull (variables[i]);
}
Copying a multi question into a different multi question
If you want to set the values of the current respondent of one multi variable (source) to a different multi variable (target), this could be accomplished with the following code:
// Generate an array with the codes of the source multi question
var sourceCodes = [‘1’,’2’,’3’];
// Generate an array with the codes of the target multi variable
var targetCodes = [‘a’,’b’,’c’];
// loop iterates through all codes from the array
for (var i = 0; i < sourceCodes.length; i++)
{
var sourceVariable =’m1_’ + sourceCodes[i];
var targetVariable =’m2_’ + targetCodes[i];
// if the value of the current respondent of the multi variable (source) is null the target multi variable will be set to null
//if the value of the current respondent of the multi variable (source) is not null, the value of the multi variable (target) will be set to the value of the multi variable (source)
if (IsNull (sourceVariable))
{
SetNull(targetVariable);
}
else
{
SetValue (targetVariable, GetValue(sourceVariable));
}
Aggregating codes from several single questions into a multi question
In this case, Q1 is a hidden multi question, while Q2, Q3 and Q4 are single questions that share the same answer list (codes) with Q1. The script will aggregate the codes from Q2, Q3 and Q4 in the multi question.
// Generate an array of all the codes available in q1 which will be used for the loop
var codes = GetCodes("q1");
// E.g. If q1==1 or q2==1 or q3==1 then q1_1 is set to true. The loop will iterate through all codes.
for(var i=0;i < codes.Length; i++)
{
if (q2==codes[i] ||q3==codes[i] || q4==codes[i])
{
SetValue('q1_'+codes[i], true);
}
}
Copying the data from questions
This is an example of a script that will copy the data from questions labeled H1 to h1.
var H1q = GetQuestionsByCategory("H1");
// this creates an array of all the questions with H1 in Question Category (question properties)
// this will loop through the questions copying the data if the question has an answer.
for (var i=0;i<H1q.length;i++)
{
var q = H1q[i]
if (GetValue(q.Name)>0)
{
h1=GetValue(q.Name)
}
}