Javascript code randomly failing | XM Community
Question

Javascript code randomly failing


Userlevel 3
Badge +11
  • QPN Level 2 ●●
  • 35 replies
In our surveys we need, for each respondent, the date in which the survey was sent to them (not the date in which they started it). For this, I have an API call in the survey flow which brings me the transaction date. Up to that point it works fine. The problem is that the transaction date comes in the format "2020-02-27 21:46:20" and I need it in "27/02/2020", so I added a javascript code to our first question that does the following:

var str = "${e://Field/transactionDate}";
var parts = str.split(' ');
parts = parts[0].split('-');

year = parts[0]
month = parts[1]
day = parts[2]

let newDate = day + '/' + month + '/' + year;

Qualtrics.SurveyEngine.setEmbeddedData("DATE", newDate);

This works fine 99% of the time, but it sometimes fails to execute and DATE ends up empty. I added UserAgent to the survey flow to see if this happens with older mobile devices but it also happens with newer ones. Does anybody know why this could be happening, and how to solve it? Thanks!

6 replies

Userlevel 7
Badge +22
Instead of double parsing try using below code:

var d = new Date("2020-02-27 21:46:20");
Qualtrics.SurveyEngine.setEmbeddedData("DATE", d.getDate()+"/"+(d.getMonth()+1)+"/"+d.getFullYear());
Userlevel 3
Badge +11
> @rondev said:
> Instead of double parsing try using below code:
>
> var d = new Date("2020-02-27 21:46:20");
> Qualtrics.SurveyEngine.setEmbeddedData("DATE", d.getDate()+"/"+(d.getMonth()+1)+"/"+d.getFullYear());

I tried that before but ended up with lots of NaN's, apparently because there are browser versions where Date doesn't support that kind of format.
Userlevel 7
Badge +27
Use moment.js instead.
Userlevel 3
Badge +11
I tried setting an embedded variable for testing at the beginning ( Qualtrics.SurveyEngine.setEmbeddedData("test", "test")) and it also comes blank when the date comes blank, meaning that the problem isn't with the parsing of the date, but that the javascript code didn't run at all for that respondent, but I have no idea why.
Userlevel 7
Badge +27
> @AxelS said:
> I tried setting an embedded variable for testing at the beginning ( Qualtrics.SurveyEngine.setEmbeddedData("test", "test")) and it also comes blank when the date comes blank, meaning that the problem isn't with the parsing of the date, but that the javascript code didn't run at all for that respondent, but I have no idea why.

Are you looking at your response data? You must initialize the embedded variables test and DATE in the survey flow for them to be captured in response data.
Userlevel 3
Badge +11
> @TomG said:
> > @AxelS said:
> > I tried setting an embedded variable for testing at the beginning ( Qualtrics.SurveyEngine.setEmbeddedData("test", "test")) and it also comes blank when the date comes blank, meaning that the problem isn't with the parsing of the date, but that the javascript code didn't run at all for that respondent, but I have no idea why.
>
> Are you looking at your response data? You must initialize the embedded variables test and DATE in the survey flow for them to be captured in response data.

Yes, I have both of them declared in the survey flow.

Leave a Reply