Check user selectIon against a lookup table, write other values in that row to embedded data | XM Community
Solved

Check user selectIon against a lookup table, write other values in that row to embedded data

  • 19 June 2018
  • 9 replies
  • 189 views

Badge +1
TL;DR Create a VLOOKUP equivalent in Qualtrics?
---------------

I have several surveys that are similar, but with various tweaks. There are about 12 surveys now, but that could grow to 30+. Instead of having several different surveys that are all similar, I'd like one survey that is customized based on the user's response to the first question. (The surveys are about sport leagues, so you'll see some of that language in here.)

For example, if the user selects "MLB" on page 1, I'd like to show survey Blocks A, B, and C. But, if the user selects "EPL," the user would see Blocks A, B, and E.

I could build a bunch of if/then logic statements into Qualtrics to accomplish this, but that seems horribly inefficient. It's also more rigid than I'd like, since when I make changes in the future I might want the "EPL" respondents to see Blocks A, D, F, and K.

My idea is to create an embedded field that corresponds with each Block (e.g., "blnShowBlockA", "blnShowBlockB", "blnShowBlock_"). If the Block should be shown to respondents, that field will get flipped to TRUE. Then, in the survey flow, I'll put branch logic before every Block; if the blnShowBlock_ variable associated with that Block is TRUE, Qualtrics will show that Block. But, how do I write each TRUE where it's supposed to be...

My thought is to create a lookup table with Blocks across the top and the sport leagues down the left-most column. Then, I could update just this table as the surveys changed and different Blocks had to be shown.
League | BlockA | BlockB | BlockC | Block...
MLB | TRUE | TRUE | TRUE ...
EPL | TRUE | TRUE | FALSE ...
NCAA | TRUE | FALSE | FALSE ...

Can I create a lookup table like this? And if so, how do I read from it and write the data back into an embedded data field?

One idea I had was creating a "contact list" with the leagues as contacts and a bunch of custom fields. That would probably work as a workaround, but there are several other reasons why I don't like that approach.

Could I just create this "table" as a matrix in JavaScript? Then I could iterate through all the entries and check to see if it matches what the user picked? I've used JavaScript, but I'm not great at it.

Of course, if there's a completely different approach I should use, I'm open to ideas.

Many thanks,
David

(for the record, I actually don't like VLOOKUP in Excel. I use INDEX/MATCH exclusively, but more people know VLOOKUP, which is why I used it in the TL;DR)
icon

Best answer by TomG 19 June 2018, 03:36

View original

9 replies

Userlevel 7
Badge +27
Use a JavaScript object to define your flags:
```
var table = {
MLB: { A: true, B: true, C: true, etc.},
EPL: {A: true, B: true, C: false, etc.},
...etc...
};
```
Then pipe the league into the JavaScript to lookup the league in your table (e.g., `table["${e://Field/league}"]`), and loop through the league's block object and set your embedded data variables appropriately.
Badge +1
Thank you for the incredibly quick reply!

This idea seems simple enough (after I did some reading and learned about for... in loops), so I'll give it a try when I get back to the survey building. Thanks again!
Userlevel 7
Badge +27
I would use jQuery.each for the loop.
Badge +1
Thanks! I haven't used that command before, so I'll read up on it.

(I first used jQuery when doing coding for a different Qualtrics survey a month ago. That's how ignorant I am about JavaScript. But, thanks to your help now, I'm improving quickly!)
Badge +1
<p>TomG - Thanks again for your help. Just that little bit gave me enough to get started. I've spent around 50 hours this week on just the opening page of my survey, but I've learned a ton about JavaScript, variables, objects, jQuery, etc. I did a lot more than this lookup table, but that is what got me rolling and made me want to do a lot more along the way.</p>

<p>For future readers, below is a little bit of what I did. I know it isn't perfect, but I couldn't have explained this last week (much less written it), so I'm pretty pleased. Of course, I'm very open to suggested improvements.</p>
<p>First, I store the data table as an object. Below is an example of tbljsLeagues, but there were actually multiple data tables (I was getting cocky).</p>
<pre><code>
var tbljsLeagues = {
lgID_001: { lgID:1, lgSport:"American football", lgSlug:"s_m_afb_ncaad1a", lgHasProRel:false},
lgID_002: { lgID:2, lgSport:"Ice hockey", lgSlug:"s_m_hok_nhl", lgHasProRel:false},
};
</code></pre>
<p>Next, I check the user's answer to a multiple choice question. Those answer choice values have been re-coded to match the lgIDs in the tbljsLeagues table.</p>
<pre><code>
var questionObject = Qualtrics.SurveyEngine.getInstance(paramQuestionID);
var currentResponse = questionObject.getSelectedChoices()[0];
var choicesObject = questionObject.getQuestionInfo().Choices;
var thisChoiceObject = choicesObject[currentResponse];
intLgID = parseInt(thisChoiceObject.RecodeValue);
strLgObjName = "lgID_"+("00" + intLgID).slice(-3);
</code></pre>

<p>Now that I know which "table row" I want, I can work with the data. For example, if the intLgID=2 (the user selected the NHL), the following both return "s_m_hok_nhl": <code>tbljsLeagues[strLgObjName].lgSlug;</code> and <code>tbljsLeagues[strLgObjName][lgSlug];</code>. The second way of writing this was important because it allows dynamic content for the property name.</p>
<p>I ended up writing the iteration code to read each key:value pair in the table into an object variable, then write that to Qualtrics embedded data.</p>

<pre><code>
var objDataToEmbed = {};
objDataTableRows = [tbljsLeagues[strLgObjName], tbljsSurveys[strSurvObjName]];
var objTableRow = {};

jQuery.each(objDataTableRows, function(paramTableRow) {
objTableRow = objDataTableRows[paramTableRow];
jQuery.each(objTableRow, function (paramTblProperty) {
objDataToEmbed[paramTblProperty] = objTableRow[paramTblProperty];
});
});

jQuery.each(objDataToEmbed, function(propKeyED, propValueED) {
Qualtrics.SurveyEngine.setEmbeddedData(propKeyED, propValueED);
});
</code></pre>
<p>Sorry it took me so long to get back, but I just wanted you to know how much I appreciated your guidance. It has put me down a path to making this survey much better than it would have been otherwise, and I'm grateful for you (and the internet community at large) for taking the time to help others. If you ever make it west to Asheville, I'll gladly buy you a local beer.</p>
Userlevel 1
Badge +2
dtyler - will you please attach a copy of your .qsf file so I can import and see this in action?
Badge +1
Julie - See attached.

I've learned a lot about JavaScript since writing this code, and looking back on it now I'm pretty embarrassed. I'm not doing things the right way, and I'm a little surprised this works at all! But it does work, so I hope it's helpful to you.
(don't worry about about all the IU logos... they're just placeholders)
Userlevel 5
Badge +11

hi @dtyler  @Julie_130 , just thought I’d add a comment to this thread as Qualtrics (albeit 5 years down the line!) are releasing a new Lookup task which should hopefully simplify things and will work of a supplemental table.

I’ve yet to try it out and especially keen to know if this will work in a loop task situation meaning it may allow us to update or change data (embedded fields) retrospectively instead of doing complex API stuff.

 

Hope that helps someone (in some way!) :)

Badge +5

@Rod_Pestell Oh my goodness -- that lookup feature would have saved me months of headaches and workarounds.

 

Thank you for calling it to my attention!! I will probably have that as part of all my surveys going forward.

Leave a Reply