Lua Scripting Resources

Important Update to Custom Scripting

SurveyGizmo's CustomScript Action now supports the LUA programming language.


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. Read-only scripts will continue to function; you will just be prevented from editing. The exact date on this is to be determined; we will send notifications well ahead of time.


 Go to our Legacy Scripting Documentation.

While SurveyGizmo is one of the most flexible survey tools around, we get requests for customizations that are not available out of the box. This is where JavaScript and Custom Scripting can save the day. If you have scripting chops you can use the JavaScript action or the Custom Scripting action to achieve the survey of your dreams.


[article("bodfy")]

Piping is a great way to use a respondent's answers from one question in a survey in another question on a later page. But what if you want to pipe from not one, but two or more questions into one question on a later page? We have a workaround that uses built-in tools or if you're doing something really complex you might need to use scripting.

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 (the question you are piping into) must be set up with the answer options included in the first two questions, with the built-in piping feature turned off.

Note: This script also works with image choice questions.

See the script in action an example survey.

OR

Add a survey with this script and setup to your account.

Features and functions used in this script:

The Script and Setup

The script below requires for us to plug in the questionIDs 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'. 

sourceID1 = 2
sourceID2 = 19
targetID = 18
next_pageID = 7  -- id of page to jump to if there are no options left to show
exclude_option = 'None of the above' -- 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

--get the values of what was checked at the two source questions, merge into one array
source_answers = arraymerge(getvalue(sourceID1),getvalue(sourceID2)) 
target_options = getquestionoptions(targetID, "Reporting")

hidden_options = 0
for key,answer in pairs (target_options) do --go through the target options reporting values and for every option which is not in the merged source array, remove it

  hideoption(targetID, answer, false)
  
  if (not(in_array(answer,source_answers)) and answer ~= exclude_option) then

      hideoption(targetID, answer, true)
      hidden_options = hidden_options + 1
  end
end


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

if(in_array(exclude_option, target_options)) then

   target_options_count = count(target_options) - 1

else 

   target_options_count = count(target_options)
    
end


if(target_options_count == hidden_options) then

   jumptopage(next_pageID)
  
end

Required Customizations

In the script above you will need to customize variables highlighted in yellow in order to make the script work in your survey.

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

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

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

next_pageID - 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'.