Using Loop and Merge with javascript and embedded data to show text based on input file | XM Community
Skip to main content

Hi,
I have a non-trivial scenario which I'm trying to implement in an efficient way:
I have a dataset (let's say it's stored in a csv file) in which for each row I have multiple values (for example, ID, Name, Address, Date etc.). This file may include thousands of rows so I'm trying to keep my work as automatic as possible. What I need to do is to read this dataset, and then implement 2 phases in my survey:
In the first phase, randomly select n rows from this dataset, extract their values, and then show to the user n idential questions, where the only differences are the values presented (like in a loop and merge scenario) - for example, in the first question use the name, ID, address etc. of the first chosen row, in the second the values of the second chosen etc.
In the second phase, I want to randomly select m rows (but not include the ones that were already chosen as the n in the previous phase) and then show another question m times. Again, in this case I want to extract the values of those m rows and use them in the question.
With the help of npetrov937 in the following question https://www.qualtrics.com/community/discussion/9317/randomization-without-replacement-reading-from-csv-file/p1?new=1 I understood how to randomize, and I even managed to read the data (as text) from the csv using the following code that I put in the first block js:
jQuery.get("MyCSVFileLink",   function(result){
  var arrT= result.split('\\n');
  var firstUser = arrT 2];
  var colVals = firstUser .split(',');
  Qualtrics.SurveyEngine.setEmbeddedData("TestName",colValsm1]); });

However, I'm now trying to understand how to use the values I extract in my loop and merge questions.....
I saw som answers by TomG in the following questions that gave me some direction: https://www.qualtrics.com/community/discussion/432/using-piped-text-from-loop-and-merge-in-java-script
https://www.qualtrics.com/community/discussion/432/using-piped-text-from-loop-and-merge-in-java-script
But I do not know how to use the extracted values from within the loop and merge loops.....
Should I randomize the users in the begining, then store the selected values - let's say as a contenated string in an embedded data, and then access this concatenated string from within each loop and merge question and extract the proper value? Is there any way to do it somoothly?
I don't want to create n identical questions and n embedded data fields, and then again m identical questions with m data fields - there must be a way to override the embedded data fields from within each loop of the loop and merge, with the values I extract in the begining....
Please please don't tell me that I cannot do this in Qualtrics, or that I have to do it manualy - those m and n might be huge...

Thanks a lot in advance!!!

First, it would be much better to use a database and a webservice instead of a csv to store and retrieve your data.
If you populate embedded data fields with your data (n.1.id, n.1.name, ..., n.2.id, n.2.name, ...) then you can pipe those values into your loop and merge fields: n.1 in row 1, n.2 in row 2, etc. Then in your loop id is ${lm://Field/1}, name is ${lm://Field/2}, etc.


Thanks for your response TomG!
I'm not sure I understood - what you suggesetd supports overriding the field per each loop?
In addition - could you please elaborate more about extracting the data from a database+webservice? Never done this before so any example would help here.
Thanks!


I'm not sure I understood - what you suggesetd supports overriding the field per each loop?

Yes, each loop would represent a different person. For example, you could pipe ${lm://Field/2} to display the name of the person.
In addition - could you please elaborate more about extracting the data from a database+webservice? Never done this before so any example would help here.

Start here: https://www.qualtrics.com/support/survey-platform/survey-module/survey-flow/advanced-elements/web-service/.
You would import your csv into a database table (e.g., MySQL) on a web server (e.g., web hosting account). Write a webservice script that execute a query returning n+m random rows and store the results in a json string that the script outputs. When you call the webservice script from Qualtrics it will populate embedded data fields with the contents of the json string.


Thanks a lot TomG! Will try it out..!


Thanks again TomG for your response.
I wrote a web service in Python and I'm now able to call it from within my survey flow.
I do have a follow up question - is there a way to avoid creating variables per each observation (1.id, 1.name, 1.address and then again 2.id, 2.name, 2.address etc.) and manually editing the Loop and Merge values table? I mean - is there an optimzed way to get the input json from the web service and create those variables "on the fly" when reaching the loop and merge block?
I saw one solution suggested in the following link - https://support.wharton.upenn.edu/help/qualtrics-json-service
which I can implement, but I wonder if there's a more efficient way to do this (i.e., not creating 100 * number of fields variables...)
Thanks in advance!


You need all the variables.
When I do this type of thing, I create a spreadsheet with formulas to concatenate the embedded variable names. In your case, you could list your numbers in column A your fields in one row 1, then create a formula at the intersection of your first number and column id (Something like "${e://Field/"&$A2&"."&B$1&"}"). If you create the formula correctly, you can copy it to all the other intersect cells.
Then copy all the cells with your embedded data fields to the first cell in your loop & merge setup. What may seem like an overwhelming number fields is a actually quite easy to deal with using this approach.


TomG
Will do. Thanks a lot!


Leave a Reply