Using Java to add embedded data from multiselect multiple choice questions | XM Community
Skip to main content

Can someone tell me where I’m going wrong?

Context:

I’m trying to use java script in two multiselect multiple choice questions to add a value to an embedded data field. In the first question (“What do we do well?”) the embedded data fields should all be set to 0, and then each of the selected choices (e.g. Pay, Vacation, Workload, etc.) should turn their corresponding embedded data field to 1. In the second question (What do we do poorly?”), each selected choice (same choices, but without those previously selected) should set the corresponding embedded data field to -1. 

I’m using the following java script on each question. At the moment, all my embedded data fields keep coming back 0 regardless what I answer in the survey.

Question 1 java script:

Qualtrics.SurveyEngine.addOnload(function() {
    // Get the question ID for "Good stuff"
    var questionId = this.getQuestionInfo().QuestionID;

    // Get the selected choices for the question
    var selectedChoices = this.getSelectedChoices();

    // Initialize embedded data fields to 0
    Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Pay", 0);
    Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Vacation", 0);
    Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Workload", 0);
    Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Culture", 0);
    Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Benefits", 0);

    // Set the embedded data fields to 1 based on the selected choices
    for (var i = 0; i < selectedChoices.length; i++) {
        var choice = selectedChoices        if (choice == 1) {
            Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Pay", 1);
        } else if (choice == 2) {
            Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Vacation", 1);
        } else if (choice == 3) {
            Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Workload", 1);
        } else if (choice == 4) {
            Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Culture", 1);
        } else if (choice == 5) {
            Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Benefits", 1);
        }
    }
});

 

Question 2 Java Script

Qualtrics.SurveyEngine.addOnload(function() {
    // Get the selected choices for the current question
    var selectedChoices = this.getSelectedChoices();

    // Set the embedded data fields to -1 based on the selected choices
    for (var i = 0; i < selectedChoices.length; i++) {
        var choice = selectedChoiceshi];
        if (choice == 1) {
            Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Pay", -1);
        } else if (choice == 2) {
            Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Vacation", -1);
        } else if (choice == 3) {
            Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Workload", -1);
        } else if (choice == 4) {
            Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Culture", -1);
        } else if (choice == 5) {
            Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Benefits", -1);
        }
    }
});

@Ian Brown You code run when the page load, every check box is still empty. That why the initialize code run but the code that check selected choice did nothing.

So you just need to change the first line of code to run it when page submit

Qualtrics.SurveyEngine.addOnPageSubmit(function() {

Hope this help


This was definitely part of the issue, Nam! Thank you!

Unfortunately, the negative responses still come up as 0 rather than -1. I changed the first line of code for that question too, but no change. I thought I might take out the step that initializes all the embedded data fields to 0, but that didn’t make a difference. Still no -1 comes through for the selected choices in the second question.

I thought this might have to do with the fact that the responses in the 2nd question are carried through from the 1st. So I recreated a new question that simply repeats all the same answer choices and created new code that subtracts 1 from whatever answer already exists in the given embedded data field (i.e. if 1 then 0, if 0 then -1).

This seems to have worked! The only downside is that people could select the same thing as positive and negative, but that is a small enough issue that I don’t care. Here’s the code for the second question:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
// Get the selected choices for the question
var selectedChoices = this.getSelectedChoices();
console.log("Selected Choices: ", selectedChoices);

// Function to subtract 1 from the current value of an embedded data field
function subtractOneFromEmbeddedData(fieldName) {
var currentValue = parseInt(Qualtrics.SurveyEngine.getEmbeddedData(fieldName)) || 0;
var newValue = currentValue - 1;
Qualtrics.SurveyEngine.setEmbeddedData(fieldName, newValue);
console.log("Updated " + fieldName + " to " + newValue);
}

// Subtract 1 from the embedded data fields based on the selected choices
for (var i = 0; i < selectedChoices.length; i++) {
var choice = selectedChoiceshi];
if (choice == 1) {
subtractOneFromEmbeddedData("Welcome - Pay");
} else if (choice == 2) {
subtractOneFromEmbeddedData("Welcome - Vacation");
} else if (choice == 3) {
subtractOneFromEmbeddedData("Welcome - Workload");
} else if (choice == 4) {
subtractOneFromEmbeddedData("Welcome - Culture");
} else if (choice == 5) {
subtractOneFromEmbeddedData("Welcome - Benefits");
}
}
});

 


Turns out having negative values for the key driver analysis tends to put things off the 100% scale. As such, I set up the embedded data to give all embedded data fields a value of 1, positive responses a value of 2, and negative values subtract 1 (i.e. 0).


Leave a Reply