Dynamic Option Selections/Answers to Survey Questions | XM Community
Skip to main content
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!
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]);
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).
Thanks @GrayMatterThinking!

Leave a Reply