setChoiceValueByVariableName: what to use as variableName parameter? | XM Community
Solved

setChoiceValueByVariableName: what to use as variableName parameter?

  • 5 November 2018
  • 5 replies
  • 70 views

I wish to use the setChoiceValueByVariableName API function with a question of type Text Entry - Form variant.

This function should be called as `setChoiceValueByVariableName(variableName, [subId], value)` where `variableName` is a string described as "The variable name of the choice(s) being set" in the documentation.

In the SE engine, I used to replace `variableName` with the text that appears on the left of the text field in the form.
With the new JFE engine, these lines of code no longer work (no error message, the text fields are simply no longer populated).

Does anyone know what the variableName parameter should contain when using this API function?

this.setChoiceValueByVariableName("byVariableName", "Modified answer"); // Does not work
this.setChoiceValue(2, "Modified answer"); // Works

Results in:

!
icon

Best answer by fleb 18 December 2018, 19:06

View original

5 replies

As a follow up on my own question, querying the content of the `VariableName` field from within the `Qualtrics.SurveyEngine.QuestionInfo` object shows that it contains the choice's number in string format, not the content of the `Text` field.

So this function seems to have become useless, as one needs to know the number that was affected to the choice in any case.

Unless one writes custom code to fetch the number associated with a given Text field. If anyone has already done this, I would appreciate sharing such code.
Do you still need help with this issue? If so could you clarify for me a bit more what it is exactly you are trying to accomplish?
Userlevel 5
Badge +6
And what about sending your variables to the embedded data fields and then accessing them using piped text?
Piped text is not appropriate in my case because the question is inside a Loop & Merge block. The same variable gets reused several times within a survey run.

Here is what I ended up doing:
```
var questionChoices = Object.values(qualtricsQuestion.getQuestionInfo().Choices);
var variableName = questionChoices.find(x => x.Text === text).VariableName;
qualtricsQuestion.setChoiceValueByVariableName(variableName, value);
```
`qualtricsQuestion` is the reference of the question I want to edit with javascript.
`text` contains the descriptive text for the target input field inside this question. In the case from my initial screenshot, this would contain "byVariableName".
`value` contains the value I want to record in this field.
Userlevel 5
Badge +6
> @GhisLabo said:
> Piped text is not appropriate in my case because the question is inside a Loop & Merge block. The same variable gets reused several times within a survey run.
Generally, this does not matter. The embedded fields are updated after each loop. Therefore you can use piped containing an embedded field. It will always contain the information generated in the previous loop. Here you can find how to set any text (including the piped one) as a default choice in the text entry box.
I have tried following simple code and it worked.

Qualtrics.SurveyEngine.addOnload(function()
{
var questionText = "${lm://Field/1}";
var n = "${e://Field/n}"+ questionText;
Qualtrics.SurveyEngine.setEmbeddedData("n", n);

});

In case this won't help you to solve your problem, I recommend you to
> clarify a bit more what it is exactly you are trying to accomplish
as @RDurant said.

Leave a Reply