Loop and merge from a web service comma-separated list | XM Community
Question

Loop and merge from a web service comma-separated list

  • 24 January 2020
  • 4 replies
  • 91 views

Userlevel 3
Badge +10
  • Level 3 ●●●
  • 35 replies
I have a web service that returns a comma-separated list (with number of items varying from ~10 to >100), which I can turn into an array. What I would like to do is create a loop and merge that pipes each value of this array into a question.

I imagine that I'm going to want populate a hidden question's answer choices with the values, then have the loop and merge loop through the choices. But I have no idea how to do this when the number of answer choices will be variable. What's the best way to do this?

4 replies

Userlevel 7
Badge +22
If we have fix 100 options, then we can create a hidden MC question (as you said) with that 100 option and select the options that web service returns using custom code. Then use this hidden question for loop and merge.
Userlevel 3
Badge +10
How do I populate the MC answer choices? Can I do the equivalent of <code>setChoiceAnswerValue()</code>, but to change the label rather than the subject's answer? I would really like to avoid making thousands of embedded variables by hand and piping them in, unless I absolutely need to. <code>jQuery("#"+this.questionId+" li.Selection").eq(2).text("NewText")</code> doesn't work - it changes the text, but this is not reflected in the loop and merge because I think Qualtrics doesn't know I've changed the text when I do it that way.

Also, once you have your MC question with your 100s of answer choices, how do you hide the blank ones? I played around with making a hidden question with three choices, [A, B, C], and then added <code>jQuery("#"+this.questionId+" li.Selection").eq(2).hide()</code> to the Javascript. In the future loop-and-merge, set to show only "Displayed" items, "C" is still displayed, again because I think Qualtrics doesn't know I hid it.
Userlevel 3
Badge +10
A working solution is as follows:

1. Create lots of embedded variables with a counter (e.g., "v1", "v2", "v3", etc.). Leave the values blank.


2. Get the CSV from web service, split it, loop through the array and set the embedded variable values (e.g. if your list is stored in the variable "dat", the inside of your loop would use <code>Qualtrics.SurveyEngine.setEmbeddedData( 'v' + i, dat[i]);</code>).


3. Pipe your embedded variables into a multiple choice question. Hide the question using javascript (NOT using display logic): <code>jQuery("#"+this.questionId).hide();</code>

4. Set the checkbox values to true for just the choices that are not blank (e.g. the inside of your loop would use <code>if (!Qualtrics.SurveyEngine.getEmbeddedData('v'+i)==""){this.setChoiceValue(i,true);}</code>

5. For the loop and merge, pipe in the selected answers from the multiple choice question.


The only issue with this solution is that it requires manually creating lots of embedded variables in step 1. Anyone have any thoughts on how to make that process less annoying?
Userlevel 7
Badge +22
One of the way to create lots of embedded data is to - create a contact list with that many embedded data(which can be done by using excel and importing it in qualtrics). Now in survey flow add embedded data and import it from contact list.

We can also modify QSF file by simply copy paste the part(Syntax) which creates ED in survey

Leave a Reply