Can anyone help me in make sense on the following code? | XM Community
Solved

Can anyone help me in make sense on the following code?

  • 22 July 2023
  • 2 replies
  • 35 views

Userlevel 3
Badge +5

Qualtrics.SurveyEngine.addOnload(function() {
  // Define the array to store the selected characteristics
  var selectedCharacteristics = [];

  // Loop through each item and check if it's not empty (embedded variable has a value)
  for (var i = 1; i <= 5; i++) {
    var itemValue = "${e://Field/item" + i + "}"; // Get the embedded variable value for each item

    // If the itemValue is not empty (not equal to an empty string)
    if (itemValue != "") {
      selectedCharacteristics.push(itemValue); // Add the characteristic label (embedded variable value) to the array
    }
  }

  // Calculate the resulting text
  var resultText = "";

  if (selectedCharacteristics.length == 1) {
    resultText = selectedCharacteristics[0]; // If only one characteristic is selected, no need for "and"
  } else if (selectedCharacteristics.length > 1) {
    resultText = selectedCharacteristics.slice(0, -1).join(", ") + " and " + selectedCharacteristics.slice(-1); // Join all but the last with commas, then add "and" before the last
  }

  // Set the Embedded Data with the resulting text
  Qualtrics.SurveyEngine.setEmbeddedData("resultText", resultText);
});

 

doesn’t work, what’s wrong? This codes try to get 5 embbeded variables, evaluate if they are not empty, if they have a text then put in an array, use this array to create a new embbeded variable with the combination of the each embbeded variable, if there are more than one embbeded variables with text, it joins and includes and “and” to make sense to the message and separate by commas. 

 

icon

Best answer by rondev 23 July 2023, 08:48

View original

2 replies

Userlevel 7
Badge +22

Use the below code in for loop to get the embedded data value:

 var itemValue = Qualtrics.SurveyEngine.getEmbeddedData("item"+i);

 

Userlevel 3
Badge +5

Use the below code in for loop to get the embedded data value:

 var itemValue = Qualtrics.SurveyEngine.getEmbeddedData("item"+i);

 

Thanks, that works nicely!

Leave a Reply