How do I set Default Choices using JavaScript and Embedded Data values? | XM Community
Skip to main content

I need to populate Default Choice values into a Matrix Table column using the JavaScript method "setChoiceValueByVariableName(variableName,[subId],value)". What syntax do I use for this, especially the "value" argument, which needs to point to a unique Embedded Data variable for each row ID? As an FYI, I already have an easy process to create and load these Embedded Data variables using the "[[ED:{field}:{value}]]" command line of the "Advanced .TXT" survey import functionality per here:
https://www.qualtrics.com/support/survey-platform/survey-module/survey-tools/import-and-export-surveys/
These Embedded Data variables for each Matrix Table row would all have a two character prefix like "R_" plus a unique company or stock symbol ID like "AAPL" or "MSFT". So the full variable ID would look like this: "R_AAPL", "R_MSFT", etc. The "variableName" argument of the JavaScript method above would always be equivalent to the company or stock ID (i.e., the same text as the Embedded Data variable, except without the "R_" prefix). Finally, the "subID" argument would just be the fixed column number of the Matrix Table. See the Qualtrics description of this JavaScript API method here:
https://s.qualtrics.com/WRAPI/QuestionAPI/classes/Qualtrics%20JavaScript%20Question%20API.html#properties
I have no experience with JavaScript. However, based on the facts above, the basic structure of this method should have a syntax similar to what I show below. This structure is shown for just one company right now ("AAPL") and assumes I want to populate the Default Choice values of column #2 of a Matrix Table. It also assumes that I need some sort of "get" function in the "value" argument to pull the Embedded Data value:
setChoiceValueByVariableName("AAPL", 2, Qualtrics.SurveyEngine.getEmbeddedData("R_AAPL"))
I didn't see this "getEmbeddedData()" function on the API page linked above, but I did see it in another Community Forum post somewhere, which is why I included it here as my guess for the correct syntax. I have no idea whether this is correct or works. However, clearly some sort of function like this is needed to populate the "value" argument from Embedded Data that has been previously stored as fixed values. An alternative to this function might be to enter the standard Embedded Data formula syntax as a string into the final "value" argument, like this:
setChoiceValueByVariableName("AAPL", 2, "${e://Field/R_AAPL}")
So my key questions for the Community Forum here are as follows:
1) Is one of my syntax / structure guesses for the JavaScript method above correct?
2) What JavaScript do I need to envelope this method to make it fully functional? and
3) How can I implement a JavaScript loop for this method across the "Selected Choices" (2-25 max) for the Matrix Table rows, each with a different company ID driving the "variableName" argument and the "value" argument?
Note that I have already applied "Carry Forward Choices" to the rows of the Matrix Table, so the loop part of JavaScript should only have to loop through a maximum of 25 company IDs (row choices) rather than the full list of thousands per a previous multiple choice question. If I cannot do a loop, then I suppose I could do a brute force JavaScript to set the Default Choice values for every company ID, one JavaScript line per company ID. However, that would clearly not be the optimal way to code here and could cause performance and code maintenance issues.

Be the first to reply!

Leave a Reply