JavaScript not working | XM Community
Skip to main content

I have created JS to define a variable by picking the highest score between multiple items. I then add these new variables as embedded data in the survey flow to include an an email trigger score report. The JS does not seem to run as these defined variables are not factored into my scoring at the end. I am wondering if anyone had a suggestion as to what could be off.


Qualtrics.SurveyEngine.addOnload(function()
{

var D2 = Math.max(parseInt("${gr://SC_080RbHTrsT1D3dX/Score}"), parseInt("${gr://SC_bxT0sMdAzcuvVwp/Score}"), parseInt("${gr://SC_0qdtqn6ZDef4M3r/Score}"));
Qualtrics.SurveyEngine.setEmbeddedData("D2", D2);

var D3 = Math.max(parseInt("${gr://SC_d6FNACqSr3a57F3/Score}"), parseInt("${gr://SC_eyuzFq3CuB0eKjz/Score}"));
Qualtrics.SurveyEngine.setEmbeddedData("D3", D3);

var D4 = Math.max(parseInt("${gr://SC_b286v30r9zo0naZ/Score}"), parseInt("${gr://SC_2bDQfrW19FnTcXj/Score}"), parseInt("${gr://SC_e2rxOogyrh9w3f7/Score}"), parseInt("${gr://SC_9RMkPlGRr2G1MDr/Score}"));
Qualtrics.SurveyEngine.setEmbeddedData("D4", D4);

});

Make sure you defined the embedded data fields in the survey flow prior to the block that contains the JS.
Your code looks ok to me. You might try adding console.log(D2), etc. after assigning each variable, them take the survey and inspect the console when you get to question to see what the values are.


Hi Tom,
Thanks so much for your suggestion. I moved the embedded data fields in the survey flow prior to the JS block. This did not solve my problem.
To your second suggestion, I did this, and it IS showing the correct variable value, but the value is somehow not getting assigned in the survey flow within Qualtrics.
I chatted with a Qualtrics support person and they said that my survey flow looked good and did not have any changes to suggest. They looked at my JS Question API, and said it looks like the variables are being assigned as a string, but they are defined as an integer. Isn't this done with my “parseInt” function? (I am a JS newbie and this code was written with help from a colleague)
Any suggestions would be greatly appreciated.


Try converting the number to text like this:
Qualtrics.SurveyEngine.setEmbeddedData("D2", D2+"");


image.pngI did as suggested and it did not work.


Just to clarify, if you go to Data & Analysis -> Tools -> Choose Columns -> Embedded Data are D2, D3, and D4 not in the list of embedded data fields?


They are listed as Embedded Data fields. I have attached the QSF file.
UCLA_PTSD_Reaction_Index_Child_Report(1).qsf


Hi,
I hope you are not checking the data for the embedded data variables by generating the test cases, if you want to check the correct data is getting stored then you will have to check using the preview link that should be qualified.
JS won't get execute in generate test cases.


Correct, I have been checking by manually completing the survey.


https://www.qualtrics.com/community/discussion/comment/28468#Comment_28468And if you select/check them, do they have values or are they blank?


They are blank.


KKI_CCFTS ,
I took a look at your survey. The issue is that your JavaScript is never running because the question the JavaScript is attached to is never displayed due to display logic. If you hide a question with display logic the question is never sent to the browser and the JS never runs.
Also, you can't set and pipe embedded variables on the same page. So, if those questions did display, the piped values would be from before the JS ran (blank).


Ah, thank you for clarifying! Given that the code is needed for all questions in my survey, do you have a suggestion of a way to embed the JS the code block after all the items have been populated? Is there a way to do this without having to add another question or have a blank page. Thank you!!!


First, do all the calculations in the JS for one question (instead of three) and click the next button at the end of the JS. Add a lot of padding to the top of the question text to push the question text and buttons off the bottom of the screen.


Thank you for your response. I understand the first part and will include all the JS calculations in one question. Should the JS be behind the questions that will be populated by respondents or a separate question that is the blank descriptive text in the same block? My apologies that I don't quite understand the second half of your response-if you could please clarify.
To prevent the need for an extra page, my college suggested having the JS under the "on page unload” line of JS, but this did not work.


KKI_CCFTS ,
The JS has to be attached to a question on a page after the last scored question. The block is irrelevant. It could be attached to a question populated by respondents as long as it meets the requirement above. Also, the embedded variables must defined in the survey flow, prior to the block that contains the JS calculations.
Assuming the question with the JS calculations remains a descriptive text question on its own page, you would use JS to click the Next Button after the calculations are complete. You would add padding (white space) to the top of the question text so the respondent just sees blank page for the fraction of a second while the calculations are done.


Got it-was able to get it working. Thanks so much!


Leave a Reply