Embedded Data Randomly Not Set by JavaScript | XM Community
Solved

Embedded Data Randomly Not Set by JavaScript


Hello,
I have a survey where a respondent is to pick from a list of options in a multiple-choice dropdown list. I have JavaScript behind this question that sets some embedded data fields to various values based on the selection. The JavaScript is in the .addOnUnload area. Basically, it's a variable list, plus the code to set the embedded data:
Qualtrics.SurveyEngine.setEmbeddedData("ApproverEmail", list[jQuery("#"+this.questionId+" select").val()]);
That's done a few times for different data fields.
This is all working quite well most of the time, but sometimes the embedded data fields are not set even though the respondent finishes the survey. I do have Force Response on the question. Qualtrics Support is not aware of any issues with JS on questions and recommended that I ask here. Is there something obvious I have done wrong? Is the JavaScript in the right place?

icon

Best answer by SurajK 29 June 2020, 17:34

View original

11 replies

Userlevel 7
Badge +22

Is the drop down question force response?

Yes, that's right. Force Response is set for that question. I can see a selection was made in the data, but the embedded values were not set by the JS for some reason on a few responses.

Userlevel 7
Badge +22

https://www.qualtrics.com/community/discussion/comment/27300#Comment_27300What is length or how many number of elements are present in list and how many options are present in drop down?

It's quite long. There's 147 options in the drop-down and as many in three of the variable lists that are setting three different embedded fields. Then there's two other embedded fields that are set only for a few of the options selected.
The idea was, instead of having and maintaining 147 different email trigger actions, we would set some of the info for the email in this JS and switch to one email trigger action. Mostly it works... but not these seemingly random times.

Userlevel 5
Badge +4

What is the list in here, is it defined? I suggest to use the code in page submit. The below code works perfectly fine.
Qualtrics.SurveyEngine.addOnPageSubmit(function(type)
{
if(type == "next")
{
Qualtrics.SurveyEngine.setEmbeddedData("ApproverEmail",jQuery("#"+this.questionId).find("select").val());
}
});

Thank you, SurajK. Is this looking about right? I have multiple lists--do I need to refer to them in each .setEmbeddedData? Sorry for so many questions!
Qualtrics.SurveyEngine.addOnPageSubmit(function(type)
{

//This is the list of approver emails
var list = {
1: "example value 1",
2: "example value 2",
};

if(type == "next")
{
Qualtrics.SurveyEngine.setEmbeddedData("ApproverEmail",jQuery("#"+this.questionId).find("select").val());
}

//This is the list of approver names
var list2 = {
1: "example value 1",
2: "example value 2",
 };

if(type == "next")
{
Qualtrics.SurveyEngine.setEmbeddedData("ApproverName",jQuery("#"+this.questionId).find("select").val());
}

//etc.
});

Userlevel 5
Badge +4

https://www.qualtrics.com/community/discussion/comment/27312#Comment_27312Ok, in this case you will have to specify the respected list array and no need to write the type==next condition for each list, use the below code,


Qualtrics.SurveyEngine.addOnPageSubmit(function(type)
{


     
     if(type == "next")
{
//This is the list of approver emails
     var list = {
1: "example value 1",
2: "example value 2",
     };


Qualtrics.SurveyEngine.setEmbeddedData("ApproverEmail",list[jQuery("#"+this.questionId).find("select").val()]);


     //This is the list of approver names
     var list2 = {
1: "example value 1",
2: "example value 2",
     };


Qualtrics.SurveyEngine.setEmbeddedData("ApproverName",list2[jQuery("#"+this.questionId).find("select").val()]);


     //etc.
     }
});

Got it! I will try this out and see how it goes. Thank you so much!!! :)

I've modified my code, and it seems to be working so far. Thank you again. I assume this event always triggers and that the unload event could possibly be avoided somehow (perhaps by someone closing their browser instead of leaving the page first?). Is that correct? I'm just curious and would like to understand more.

Userlevel 5
Badge +4

The unload function works when you click next button or even when you click back button. This may be problem in case of back and forth. The page submit function can make work in case of next click or even if you want to make it work when back click. Also, you can write other code as well in case of back click. I prefer page submit function.

I think I understand. Thank you!

Leave a Reply