Default Choice Conditions: Embedded Data and Current Date | XM Community
Skip to main content

I’m using an anonymous link and have a URL query string that passes embedded data for some users. The embedded data that is being captured is a date, and I’m looking to set a default choice for a response within a form field. If the embedded data is available, I want the default choice to be the embedded data field, and if no embedded data exists, I want the default choice to be the current date. I don’t believe I’m able to set conditions for default choices, but javascript should be able to inject one or the other based on the condition embedded data exists. Looking for support in building that code. Thanks! 

you can use the following code as an example to set the default choice based on the embedded data:

 

Qualtrics.SurveyEngine.addOnload(function() {
  // Replace 'embeddedDateField' with the name of your embedded data field for the date
  var embeddedDate = "${e://Field/embeddedDateField}";

  // Get the current date
  var currentDate = new Date();

  // Convert the embedded date to a Date object
  var parsedEmbeddedDate = new Date(embeddedDate);

  // Check if the embedded date is valid and set the default choice accordingly
  if (parsedEmbeddedDate.toString() !== "Invalid Date") {
    // Set the default choice to the embedded date if it exists
    this.setChoiceValue('yourQuestionChoice', embeddedDate);
  } else {
    // Set the default choice to the current date if the embedded data doesn't exist or is invalid
    this.setChoiceValue('yourQuestionChoice', currentDate.toLocaleDateString());
  }
});

 

Make sure to replace 'embeddedDateField' with the actual name of your embedded data field that contains the date, and 'yourQuestionChoice' with the choice value you want to set as the default.

 


Thanks @Shivamvig_sv - how do I set the ‘yourQuestionChoice’ value if the text entry is part of a form field? Is there a reference to the QID?


You don’t need to use javaScript here. Just break the block and set an embedded data with default value of your choice. Then add a logic where you can check if the date data is empty or not and if it’s not empty replace the variable you created earlier with this one and pipe it. Simple!


Leave a Reply