Utilising JavaScript for 2-tier Org Hierarchy function | XM Community
Skip to main content

I am experimenting with Qualtrics EX program for employee pulse surveys. 

These are run via an anonymous link that is distributed to all employees to complete. The first question that employees are asked is to select their “Group” and “Division” these are pre-set via an org hierarchy.

The problem with this is, while you can force a response to the question, you cannot force a response on both levels. As in, if someone was to select their “Group”, then they are not forced to select their “Division” because the response requirement has been met. This can create incorrect results if someone fails to select their “Division”

The only solution I can see while maintaining the Anonymous link, is to utilise JavaScript to create a 2nd force response in the one question, but I can’t seem to get it to work.

This is what has been created so far. I have replaced the Group and Division names with dummy and divvy for this example. Note not every “Group” has a Division, Dummy3 is just a Group. “orgname” is also replaced with the actual org title. 

Qualtrics.SurveyEngine.addOnload(function()
{
    // Hardcoded hierarchy object
    let hierarchy = {
      "Orgname": {
        "Dummy": {
          "Divisions": "Divvy1", "Divvy2"]
        },
        "Dummy2": {
          "Divisions": r"Divvy1"]
        },
        "Dummy 3": {
            "Divisions": ]
                };
   
    // Disable the next button
    this.disableNextButton();

    // Question click handler
    this.questionclick = function(event, element)
    {
        if (element.type == 'radio')
        {
            var choiceNum = element.id.split('~')2];
            alert('You clicked on choice '+choiceNum);

            // Get the respondent's answers
            let groupAnswer = "${q://QID1/ChoiceGroup/SelectedChoices}";
            let divisionAnswer = "${q://QID2/ChoiceGroup/SelectedChoices}";

            // Check if the group has divisions
            if (hierarchyi"Orgname"] groupAnswer]2"Divisions"].length > 0) {
              // If the group has divisions, check if a division was selected
              if (divisionAnswer === "") {
                console.log(`A division must be selected for the group ${groupAnswer}.`);
                this.disableNextButton();
              } else {
                console.log(`Response recorded for ${divisionAnswer} in ${groupAnswer}.`);
                this.enableNextButton();
              }
            } else {
              // If the group does not have divisions, record the group answer
              console.log(`Response recorded for ${groupAnswer}.`);
              this.enableNextButton();
            }
        }
    }
});
 

Be the first to reply!

Leave a Reply