Piping From Two Questions Into One

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.

Sometimes it is appropriate to ask two separate questions on slightly different themes, but ask a follow-up question about what the respondents select as a combination. In this case, we set up a script which filters the answer options displayed in the target question based on the answers selected in the previous two questions.

For this to work the target question must be setup with answer options included in the first two questions, with automatic piping turned off.

Note: This script also works with image choice questions because they return answers as an array.

Check out the first 3 pages of this example survey!

OR

Add this survey to your account!

Estimated implementation and testing time: 5-10 minutes

This script uses the following custom scripting functions:

  • sgapiGetValue
  • sgapiArrayMerge
  • sgapiGetQuestionOptions
  • sgapiIn_Array
  • sgapiRemoveOption
  • sgapiCount
  • sgapiJumpToPage

The Script

The script below requires for us to plugin the question ids of the two source questions, the target question, the id of the next page to jump to (should there be nothing to display on the page with the target question), and, optionally, an option to be displayed in target question irrespective of whether it was ticked in either of the source questions. In this example, the answer label for that option is 'None of the above'. 

%%source_id1 = 2; //checkbox question 1 id
%%source_id2 = 19; //checkbox question 2 id
%%target_id = 18; //target checkbox question id
%%next_page_id = 7;  // id of next page to jump to in case there are no options left to show
%%exclude_option = 'None of the above'; // this is optional in case you have an option in the target question which you want to keep irrespective of if it was checked in either of the source questions, if you do not, you could just set this to be = '';


%%source_answers = sgapiArrayMerge(sgapiGetValue(%%source_id1),sgapiGetValue(%%source_id2)); //get the values of what was checked at the two source questions, merge into one array
%%target_options = sgapiGetQuestionOptions(%%target_id, "Reporting");

%%hidden_options = 0;
foreach(%%target_options as %%answer) { //go through the target options reporting values and for every option which is not in the merged source array, remove it
   

   if(!(sgapiIn_Array(%%answer,%%source_answers)) && (%%answer != %%exclude_option)) {

      sgapiRemoveOption(%%target_id, %%answer);
      %%hidden_options +=1;
   }

}

//if there are no options left to show, skip to the next page  

if(sgapiIn_Array(%%exclude_option, %%target_options)){

   %%target_options_count = sgapiCount(%%target_options) - 1;

} else {

   %%target_options_count = sgapiCount(%%target_options);
   
}


if(%%target_options_count == %%hidden_options) {

   sgapiJumpToPage(%%next_page_id);

}

In the script above you will need to customize variables highlighted in yellow in order to make the script work the way you'd like.

Required Customizations

%%source_id1 - This variable will indicate the question ID of the first source Checkbox question.

%%source_id2 - This variable will indicate the question ID of the second source Checkbox question.

%%target_id - This variable will indicate the question ID of the target Checkbox question.

%%next_page_id - The page to jump to if no options are selected in the checkbox question (and therefore the page with the textbox list question is skipped).

%%exclude_option - the answer label of an option which you wish to display in the target question irrespective of whether it has been ticked in either of the source questions. Typically an exclusive option like 'None of the above'.

Scripting and Other Custom Solutions

We’re always happy to help you debug any documented script that is used as is. That said, we do not have the resources to write scripts on demand or to debug a customized script.

If you have customization ideas that you haven't figured out how to tackle, we're happy to be a sounding board for SurveyGizmo features and functionality ideas that might meet your customization. Beyond this, you might want to consult with someone on our Programming Services Team; these folks might have the scripting chops to help you to achieve what you are looking for!