Simple layout & simple layout | XM Community
Skip to main content

I have java script in a Lifecycle survey to convert birth date to an age category.  The script works perfectly in the older survey layouts, but no in the Simple.  I know my code works as it’s been tested. My JS

Qualtrics.SurveyEngine.addOnReady(function()
{
    /*Place your JavaScript here to run when the page is fully displayed*/
    var current_date = "${date://CurrentDate/DS}";
    var current_year = current_date.substr(0,4);
    var dateOfBirth = "${e://Field/BirthDate}"; // 28/05/1970
    var dateOfBirth_year = dateOfBirth.substr(6,4); // takes 4 characters counting from the 6th character so would retunr 1970
    // var dateOfBirth_year = "${e://Field/Year Of Birth}"; // Used if the year is stored
    var captureAge = parseInt(current_year) - parseInt(dateOfBirth_year);

    var ageGroup;
                // Set group value for ageGroup
    if (captureAge < 20) {
          ageGroup = "a. younger than 20 years";
    } else if (captureAge < 30) {
          ageGroup = "b. 20 - 29 years";
    } else if (captureAge < 40) {
          ageGroup = "c. 30 - 39 years";            
    } else if (captureAge < 50) {
          ageGroup = "d. 40 - 49 years";            
    } else if (captureAge < 60) { 
          ageGroup = "e. 50 to 59 years";
    } else if (captureAge > 60) {
           ageGroup = "f) 60 years and older";
    };
       var surveyMonth = moment().format('MMMM'); 
  var surveyYear = moment().format('YYYY'); 
 
  Qualtrics.SurveyEngine.setEmbeddedData('Survey Month', surveyMonth);
  Qualtrics.SurveyEngine.setEmbeddedData('Survey Year', surveyYear);
    Qualtrics.SurveyEngine.setEmbeddedData('Age Group', ageGroup);

 

And the script in the Look & Feel>.Header: <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script><script>
    moment().format();
</script>

 

I have completed surveys in a project with the above script and when i change the layout to older versions, the data captures correctly in the D&A tab, but when I change back to Simple Layout, its blank.  HELP!

Your code isn’t complete, so I can’t be sure of the issues.

At first glance setEmbeddedData is depreciated in the simple layout. Please see https://api.qualtrics.com/82bd4d5c331f1-qualtrics-java-script-question-api-class


Um, I am not an expert at writing script so if you are able to explain in laymens terms what I need to do that would be great.


@karen_split-pin,

I’ve answered this question before. In addition to setEmbeddedData, moment.js is also deprecated.

This calculates age and will get you  most of the way there. You’ll just need to add a bit of code for age group.


Leave a Reply