setEmbeddedData not writing back to Qualtrics | XM Community
Skip to main content
Hey guys,



I do not understand why this simple script doesn't work. Makes logical sense; doesn't work.



Here's the code:



`Qualtrics.SurveyEngine.addOnload(function()

{

var SurveyYear = Qualtrics.SurveyEngine.getEmbeddedData("SurveyYear");



// Convert from EmbData String to Int

SurveyYear = parseInt(SurveyYear);

// Calculate Year after SurveyYear

var NextYear = SurveyYear + 1;

// Convert back to String to set NextYear EmbData

NextYear = NextYear.toString();



Qualtrics.SurveyEngine.setEmbeddedData("NextYear", NextYear);



});`



I'm trying to use variables to set the Survey Year instead of hard-coding the year, which is common practice here at my shop. I want to have my users pass in the current survey year when they upload their contacts panel. My EmbeddedData field is named SurveyYear, which will this year equal 2019. I use JavaScript (which I'll eventually put in a <script> tag in my Header) to do the math to add 1 to the year to the SurveyYear, set the result to NextYear, and write that back to a blank EmbeddedData field which I've created in the Survey Flow, but not declared a value.



The only line that doesn't work is the Qualtrics.SurveyEngine.setEmbeddedData("NextYear", NextYear);.



I've tried using these combinations:

- this.setEmbeddedData("NextYear", NextYear)

- Qualtrics.SurveyEngine.setEmbeddedData("NextYear", "2020")

- Qualtrics.SurveyEngine.setEmbeddedData("NextYear", 2020)

- Tried function in addOnload, addOnReady



What am I missing? Does setEmbeddedData not work on the jfe platform? Thanks for any info you can give.



-Jeremy
I don't see anything wrong with it. Have you looked to see if there are any errors in the browser console?



An easier way to set those variables is in a survey flow embedded data block:

```

SurveyYear = ${date://CurrentDate/Y}

NextYear = ${date://OtherDate/Y/+1 year}

```

Or, if SurveyYear is coming from a contact list:

```

NextYear = $e{ e://Field/SurveyYear + 1 }

```
Hi @JeremyK,

is also NextYear defined in the survey flow? It has to be. Otherwise, your script looks OK. Have you tried to check the console log? (Press F12 to see it.)
Hey guys, thanks for the responses. I did check the console and I was writing out the answers to SurveyYear, which I set in the url (&SurveyYear=2019) to simulate what the user will do in the contact panel, as well as to confirm that my script can pull in the embData with getEmbeddedData, which all works. I will definitely update NextYear to do the calculations inside the EmbeddedData field instead of having an extra step.



Fleb, I declared NextYear in the Survey Flow, but I didn't give it a value since the year will change. I tried letting survey flow handle the math, but it's not capable of calculations, I don't believe.



No errors in the script, but setEmbeddedData just doesn't write back to the survey as far as I can tell. I wondered if the adOnload ran too early or too late, so the object is already created and the NextYear is null, then the JS runs and updaets NextYear from null to 2020, but maybe wasn't updating the text on the page? My test is a Textbox in my welcome page that has Next Year: ${e://Field/NextYear} and I'm expected it to update if the setEmbeddedData code runs successfully. Does that sound right to you guys?



Also to be noted, I can update the NextYear in the URL and that works great, just having the problems setting it with the JS API.



Thanks to both of you, I was so frustrated this afternoon.
> @JeremyK said:

> I tried letting survey flow handle the math, but it's not capable of calculations, I don't believe.

You can do calculations in the survey flow as I showed in my previous post:

```

NextYear = $e{ e://Field/SurveyYear + 1 }

```

> My test is a Textbox in my welcome page that has Next Year: ${e://Field/NextYear} and I'm expected it to update if the setEmbeddedData code runs successfully. Does that sound right to you guys?

That is your problem. You can't set and pipe an embedded data field on the same page. You can't pipe it until the next page (after the Next button has been clicked). So, NextYear is being set, you just can't pipe it until you get to the next page. If you need to update an input or html on the same page that contains the JS where you calculate the value, you need to do it with JavaScript (e.g., `jQuery(".InputText").val(NextYear);`).
That's it, Tom, that was my problem. I was trying to create, calculate, and use my result all in the same step. I checked and verified the setEmbeddedData was working correctly and I could use NextYear on the next page in the survey. However, your first post here is what I will be putting in production, doing the calculations in the Survey Flow, and populating them throughout the survey.



Thank you both!

So glad I found this. Was going insane trying to figure out why the setEmbeddedData was not writing back to Qualtrics. Turns out all I needed was a page break in between the set and pipe. Thanks for this!


Hi
Same problem, have put a page break after the setEmbededData - nothing on the csv afterwards?
And it runs twice in Preview mode, is the second run overwriting the data from the first?
After the first setEmbededData a getEmbededData shows the data as being there - so it does write it out?


https://community.qualtrics.com/XMcommunity/discussion/comment/48930#Comment_48930You need to add the embedded data fields to the survey flow prior to the block that contains the JS. Qualtrics only saves embedded data fields defined in the survey flow to response data.


Hi Tom
The embedded Data is predefined, I just set it to a calculated value in question 1.
It only works if I have a block with a text/graphic afterwards or another question.
Then I have the end of survey block which does not have anyway of saving the embedded data..


Leave a Reply