Unable to save or update embedded data variable from JS | XM Community
Solved

Unable to save or update embedded data variable from JS

  • 7 August 2019
  • 5 replies
  • 222 views

Userlevel 3
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!
icon

Best answer by iboafra 8 August 2019, 00:21

View original

5 replies

Userlevel 7
Badge +33
i think you have to move this element after block in survey flow.
Userlevel 3
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.
Userlevel 3
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.

!
Userlevel 3
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:

!

!
Userlevel 3
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