Update Hidden Value If Question Answers Do Not Match

Important Update to Custom Scripting

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


  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.

We have a feature that will verify two textbox questions on the same page match (Example: Verify that the respondent enters the same email address twice). 

This code snippet is used to check to see if the answers to any two questions in the survey match. If the answers to these questions match, a hidden value is populated with "match".  If the answers to these questions do not match, a hidden value is populated with "flag".

Add a Custom Script action and copy and paste the following code. Make sure the action follows the question from which the Hidden Value pulls. The hidden value can be anywhere in the survey in relation to the script, even the same page!

This scripting option uses the following features:

Setup

1. Start by building the survey and adding the duplicate questions.

We recommend creating one question, saving, and copying the question to ensure that the reporting values in both question are identical. You can move the second copied question to a later survey page.

2. Add a Hidden Value Action. We recommend adding this to the very top of Page 1, or the first page in your survey, so it is easy to identify when you are reviewing responses. 

You will not prepopulate the Hidden Value here, simply name the Hidden Value and save.

3. On the Thank You page of the survey, or on the survey page after the copied question from Setup #1, add a Custom Script Action.

Copy and paste this code into the action.

//compare two questions; if not same value, set a flag in hidden value

%%q1 = sgapiGetValue(6); //change this to the ID of your first question
%%q2 = sgapiGetValue(7); //change this to the ID of your second question

if (%%q1 != %%q2) {
  sgapiSetValue(5,"flag");
}
else {
  sgapiSetValue(5,"match");
}

4. Update the IDs and Hidden Value flags.

%%q1 - ID of the first question

%%q2 - ID of the second copied question

Hidden Value ID - in the example code above our ID is 5.

Update the text to populate in the Hidden Value field if necessary. In our example above, the Hidden Value will be populated with match if the question answers match and flag if the answers to the questions are different.

Save Custom Script Action. Test. Enjoy

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 alchemer features and functionality ideas that might meet your customization. Beyond this, you might want to consult with someone on our professional Services Team; these folks might have the scripting chops to help you to achieve what you are looking for!