Dynamic Option Selections/Answers to Survey Questions | XM Community
Solved

Dynamic Option Selections/Answers to Survey Questions

  • 10 July 2019
  • 3 replies
  • 80 views

Userlevel 3
Hello,

Is there a way to split a string and assign the corresponding tokens to a set of embedded variables?

Say I have a string that I retrieve from a web service or any external source such as:

Red,White,Blue

Is there some facility that would allow me to split that csv string into 'Red', 'White' and 'Blue' individually and add them as separate options into a survey question?

Thanks!
icon

Best answer by GrayMatterThinking 10 July 2019, 18:11

View original

3 replies

Userlevel 3
Badge +4
Hi - assuming that you have the string stored into an Embedded Data variable then you could add JavaScript to any question (after the Embedded Data is set). If your ED field was called 'myString' then the JS would look something like below. Stick it in the addOnLoad section. Note that the embedded data fields that you are writing to should already be present within the Survey Flow with no assigned value. This example assumes that you are always receiving 3 values but if this is different or fluctuates then you'll need to have enough Embedded Data variables set up to capture the maximum expected number of responses and you'll need to amend the code to loop through the array.

var myString = Qualtrics.SurveyEngine.getEmbeddedData('myString');
var stringArr = myString.split(',');

Qualtrics.SurveyEngine.setEmbeddedData('var1',stringArr[0]);
Qualtrics.SurveyEngine.setEmbeddedData('var2',stringArr[1]);
Qualtrics.SurveyEngine.setEmbeddedData('var3',stringArr[2]);
Userlevel 3
Badge +4
Also this is dependent on where the string is coming from in the first instance. Is it another survey question or is it a web service? If a web service then you should be able to split the string into the required array before the web service passes the values back into Qualtrics (at which point they can be assigned to your Embedded Data variables).
Userlevel 3
Thanks @GrayMatterThinking!

Leave a Reply