sgapiGetValue(%%questionID)

Important Update to Custom Scripting

SurveyGizmo's CustomScript Action now supports the LUA programming language. Visit our NEW Lua Scripting Resources!

Legacy Custom Scripting Language Deprecation Plans 

  1. New accounts (created after October 29, 2018) will only have the option to use Lua in scripts.
  2. As of October 29, 2018 Custom Scripting Actions will default to Lua as the scripting type in the Custom Scripting Action for accounts created before this date. You will be able to switch to the Legacy Custom Scripting; though we highly encourage using Lua.
  3. In the long term, Legacy Custom Scripting Actions will be switched to read-only. The exact date on this is to be determined; we will send notifications well ahead of time.

This function returns the value (reporting value) of an answered question (or null, if not answered). For single answer questions, a single value is returned. Multi-answer questions return an array. Grid and custom group questions return a multidimensional array.

Examples

Single Answer Questions

Includes Multiple Choice, Dropdown Menu, Single Slider, Likert Scale, Dropdown Menu, NPS and all single textfield questions (Textbox, Email, Number, Date, Essay).

Single-Select Example Survey

//This prints the return to sgapiGetValue for single select questions. 
//The output will be the answered text or reporting value for the answer option. 
//%%questionid refers to the question id.

%%output .= sgapiGetValue(%%questionid);

//This outputs the question title and the answer as a sentence.

%%output .= 'The answer to question ' . sgapiGetTitle(%%questionid) . ' is ' . sgapiGetValue(%%questionid);

Multi-Answer Questions

Textbox List, Continuous Sum, Checkboxes, Slider List, Drag and Drop Ranking, Ranking Grid, and Image Select questions all return an array. The option skus are they keys and the answers are the values. 

Multi-Answer Example Survey

//This outputs the entire return for sgapiGetValue for multi-answer questions

%%output .= sgapiPrint_R(sgapiGetValue(%%questionid)); 

For a textbox list, the option skus are the keys and the answers to each textbox are the values. 

For checkbox and image select questions, the option skus are the keys and the reporting values of the answers are the values. 

For both drag and drop ranking and ranking grid, the option skus are the keys and the ranks selected are the values.

//This prints the answers to a checkbox or image select question as a sentence.

%%output .= 'The answers to '. sgapiGetTitle(%%questionid) .' are: '. sgapiImplode(',',sgapiGetValue(%%questionid));

//This prints the answers to a textbox list, slider list, and continuous sum as multiple sentences.

%%optionTitles = sgapiGetQuestionOptions(%%questionid);
foreach (sgapiGetValue(%%questionid) as %%optionsku => %%reportingvalue){
  %%output .= 'The answer to question ' . sgapiGetTitle(%%questionid);
  %%output .= ' row ' . %%optionTitles{%%optionsku} .' is ' . %%reportingvalue . '<br>';
}

//This prints the ranks from a ranking type question

%%optionTitles = sgapiGetQuestionOptions(%%questionid);
foreach (sgapiGetValue(%%questionid) as %%optionsku => %%rank){
  if (%%rank){
%%output .= 'In question '.sgapiGetTitle(%%questionid);
%%output .= ', option '.%%titles{%%optionsku}.' is ranked '.%%rank.'<br>';
  }
}

//You can also access individual answer options through the option sku. 

%%question = sgapiGetValue(%%questionid);
%%option = %%question{%%optionsku};

Grid Questions

For grid and custom group questions, the return is a multidimensional array.

Grid and Custom Group Example Survey 

//This outputs the return to sgapiGetValue for grid and custom group questions

%%output .= sgapiPrint_R(sgapiGetValue(%%questionid)); 

The row or subquestion ids are the keys and arrays of the results for each row or subquestion are the values. Because the return is a multidimensional array, you would need to use a nested foreach loop to iterate through each answer to the question. 


//This example script iterates through the return for a radion button grid, semantic differential, or dropdown menu list question and prints the answer for each row as a sentence. 

//The first foreach loop iterates through each row of a grid question
foreach (sgapiGetValue(%%questionid) as %%rowid => %%rowarray){   
//The second foreach loop iterates through all of the answers for each row   
  foreach (%%rowarray as %%optionsku => %%reportingvalue){     
  %%output .= 'Question ' . sgapiGetTitle(%%questionid);
  %%output .= ': The answer to row '. sgapiGetTitle(%%rowid).' is '. %%reportingvalue;
  %%output .= '<br>';    
  } 
} 

//This example script iterates through the return for a textbox grid, dropdown menu grid, or star ranking grid and prints the answer for each field as a sentence.

%%columnTitles = sgapiGetQuestionOptions(%%questionid);
//The first foreach loop iterates through each row of a grid question
foreach (sgapiGetValue(%%questionid) as %%rowid => %%rowarray){   
//The second foreach loop iterates through all of the answers for each row   
  foreach (%%rowarray as %%optionsku => %%reportingvalue){  
  %%output .='Question: '.sgapiGetTitle(%%questionid);
  %%output .=' Row: '.sgapiGetTitle(%%rowid);
  %%output .=' Column: '.sgapiGetTitle(%%rowid);
  %%output .=' Answer: '.%%reportingvalue.'<br>';
}
}

//This example script prints the results for each row of a checkbox grid question.

foreach (sgapiGetValue(%%questionid) as %%rowid => %%rowarray){   
  %%output .= 'Question ' . sgapiGetTitle(%%questionid) .': The answers to row '. sgapiGetTitle(%%rowid).' are '. sgapiImplode(',',%%rowarray). '<br>';
}

//You can also get the answer to an individual field 
%%question = sgapiGetValue(%%qid); 
%%field = %%question{%%rowid}{%%optionsku}; 

Grid and Custom Group Questions

For Grid questions, Custom Group, and Contact Groups, the sub-question ID can be accessed via sgapiGetValue. You can call sgapiGetValue on a custom group. It will return an array with the subquestion ids as the keys and an array for each individual subquestion for the values. You can call sgapiGetValue on a custom table as well. it will return nested arrays.

Output would be the same return as if the question was a standalone question. For instance, the subquestion of a radio button grid would return just the value of the option selected, just like an individual radio button question. The subquestion of a checkbox grid, would return an array of the all selected checkboxes with their option skus and the keys and their reporting values as the values. 

Note: To output an array the sgapiPrint_R function must be used.

Limitations

  • sgapiSetValue cannot be used to set the value of an other textbox.