Embedded data assignment not working in JavaScript | XM Community
Skip to main content

Hi,

I have a survey where I input the start and end date of my class(from the contact list). On the first question of the survey, using JavaScript, I calculate the number of days it has been since the class started and the number of days left till graduation using those dates. I assign these values to the embedded data fields “Days Since Cohort Start” and “Days Till Graduation”. Based on these values, the survey shows/doesn’t show certain questions. The embedded data fields are defined in the survey flow before the question with the Javascript. 

 

The assignment of the values to the embedded data fields is not working. The reason I am assuming this is that - 

  1. I have a console.log function in the javascript and so I am able to see that the script is running and the calculations are happening correctly
  2. When I manually set the “Days Till Graduation” the rest of the survey flow & the survey run correctly

Another confounding factor is that I have the same JavaScript code on another survey and its working perfectly fine there!

Any help would be greatly appreciated and thank you in advance!

The survey flow. The Javascript is attached to the question in the “Introduction” block

My JavaScript code (I personally don’t know JavaScript, and everything added here is using the answers from the very helpful community pages)

Qualtrics.SurveyEngine.addOnload(function()
{

var today = new Date();
console.log("today is" +today);

var graddate = new Date("${e://Field/Cohort%20Graduation%20Date}")
console.log("graddate is "+graddate);
    
var startdate = new Date("${e://Field/Cohort%20Start%20Date}")
console.log("startdate is "+startdate);

var diffgrad = new Date(graddate - today);
var diffstart = new Date( today - startdate);
var daystillgrad = diffgrad/1000/3600/24;
var dayssincestart = diffstart/1000/3600/24;


Qualtrics.SurveyEngine.setEmbeddedData('Days Till Graduation', daystillgrad);
console.log("diff grad is " + daystillgrad);

Qualtrics.SurveyEngine.setEmbeddedData('Days Since Cohort Start', dayssincestart);
console.log("diff start is " + dayssincestart);

});

 

Console output

 

Are you using simple layout in this survey? If so, I think you need to use setJSEmbeddedData (but then you need to change the embedded data field names in the survey flow).

BTW, I recommend NOT using spaces in embedded data field names. You can replace them with underscores (_).


Hi @TomG 

Thanks so much for your quick response.

I updated the code to use setJSEmbeddedData and removed spaces from the embedded data field names(both in the code and survey flow). Its still not working for me. Do I need to make any other changes to the embedded data field names in the survey flow?

 

The code now:

Qualtrics.SurveyEngine.addOnload(function()
{

var today = new Date();
console.log("today is" +today);

var graddate = new Date("${e://Field/Cohort%20Graduation%20Date}")
console.log("graddate is "+graddate);
    
var startdate = new Date("${e://Field/Cohort%20Start%20Date}")
console.log("startdate is "+startdate);

var diffgrad = new Date(graddate - today);
var diffstart = new Date( today - startdate);
var daystillgrad = diffgrad/1000/3600/24;
var dayssincestart = diffstart/1000/3600/24;


Qualtrics.SurveyEngine.setJSEmbeddedData('Days_Till_Graduation', daystillgrad);
console.log("diff grad is " + daystillgrad);

Qualtrics.SurveyEngine.setJSEmbeddedData('Days_Since_Cohort_Start', dayssincestart);
console.log("diff start is " + dayssincestart);

});


You need to prefix the survey flow names of embedded data fields that you are updating using setJSEmbeddedData.  For example, ‘Days_Till_Graduation’ should be __js_Days_Till_Graduation in the survey flow.


Hi @TomG 

It worked!!

Thank you so much - you’re amazing!


Hi @TomG 

Is this right?

I added the __js_ to both the assigned fields

What should be the names in the custom code? I left it as before, changed it to the __js_ …, but nothing worked for me.

The fields should have the __js_ prefix in the survey flow, but NOT in the setJSEmbeddedData function.


Leave a Reply