Need help with java to use embedded data to pre-fill multiple choice questions | XM Community
Skip to main content
Solved

Need help with java to use embedded data to pre-fill multiple choice questions

  • February 7, 2024
  • 3 replies
  • 257 views

Forum|alt.badge.img+2
  • Level 1 ●
  • 9 replies

Hi all -

Very thankful for this community as I am working to set up a survey that has a lot of answers pre-filled from previous responses. I’ve already found and successfully employed a javascript code to pre-select multiple choice question options based on embedded data; however, I am now not able to duplicate it for some reason.

The code I used was:


Qualtrics.SurveyEngine.addOnReady(function()
{
    if ("${e://Field/InvestorRole}".includes("Majority")) this.setChoiceValue(1, true);
    if ("${e://Field/InvestorRole}".includes("Minority")) this.setChoiceValue(2, true);
  

});

 

I think tried to use it for the next question, reolacing the items highlighted and adding rows as needed (and then, of course, going up in numbers on set choice value - 3, 4, etc.). Now… nothing. Any ideas? I’m very confused. I double checked embedded data was entered properly, no case differences, changes published, etc.

Best answer by ahmedA

Sometimes the choice numbers can go out of hand.

You could try the following:

Add this before the if statements: const choices = this.getChoices();

Then you can select the appropriate choice by this.setChoiceValue(choices[0], true);  and so on.

 

Another possible issue could be if you are previewing the block, for ED to work, you need to preview the survey.

3 replies

Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • 2050 replies
  • Answer
  • February 7, 2024

Sometimes the choice numbers can go out of hand.

You could try the following:

Add this before the if statements: const choices = this.getChoices();

Then you can select the appropriate choice by this.setChoiceValue(choices[0], true);  and so on.

 

Another possible issue could be if you are previewing the block, for ED to work, you need to preview the survey.


Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • 9 replies
  • February 9, 2024

Thanks @ahmedA! That seems to have worked! Am I right in understanding the choice numbers do not restart at 1 for later questions, then? Just trying to understand what it means by “choice numbers getting out of hand.” Will this same code work for all 15 or so questions I need to use it on?

Really appreciate your help!

 


Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • 9 replies
  • February 9, 2024

Also, does case matter?