Unable to save or update embedded data variable from JS | XM Community
Skip to main content
Hello Folks,



I have a simple one-question survey:



!





and I've added an embedded variable, var1, at the beginning of the survey flow.



!





Using the following JS code, I am not able to successfully update var1.



!





The call to 'alert' shows this empty/un-updated value of var1.



!





I've followed what other folks have commented here: https://www.qualtrics.com/community/discussion/1928/setting-embedded-data-using-javascript but I have not been successful.



Thank in advance for your feedback and input!
i think you have to move this element after block in survey flow.
Hello @bansalpeeyush29. Sorry for being dense but what do you exactly mean by "move this element after block in survey flow"?



Thanks for looking into my question.
Just to try it out, I switched the order of the embedded variable block and the question block to see if it will make a difference. I get the same error.



!
To add more context in my testing, I reverted the order of the embedded data block and the question block, and I also initialized var1 to 50



!



This scenario proves that I can access the declared and initialized var1 variable (in addOnUnload function), however, it's the call to updating the embedded variable (in addOnload function) that is not working :(



I'm expecting that tmpVar2 would have a value of 100 after the call to Qualtrics.SurveyEngine.setEmbeddedData('var1', '100');



```

Qualtrics.SurveyEngine.addOnload(function()

{

var tmpVar1 = "${e://Field/var1}";

Qualtrics.SurveyEngine.setEmbeddedData('var1', '100');

var tmpVar2 = "${e://Field/var1}";

alert('addOnload:\\ntmpVar1 = ' + tmpVar1 + '\\ntmpVar2 = ' + tmpVar2);

});



Qualtrics.SurveyEngine.addOnReady(function()

{

/*Place your JavaScript here to run when the page is fully displayed*/

});



Qualtrics.SurveyEngine.addOnUnload(function()

{

var tmpVar = "${e://Field/var1}";

alert('addOnUnload, tmpVar = ' + tmpVar);

});

```

Pop-up alerts generated by code above:



!



!
I found @TomG's answer https://www.qualtrics.com/community/discussion/comment/15517 to be the answer to my question. When he said "You can't set and pipe an embedded data field on the same page", I checked var1 on a next question block, the variable was indeed updated.

Leave a Reply