Community Groups are officially here!
We've released Groups, a new feature that enables us to connect community members of similar industries and interests in a shared, private space. You can check out all of the details here, including information about who can join, how to join, and what Groups are currently offered. Please leave your feedback through this Community Groups Feedback Survey.

setChoiceValueByVariableName: what to use as variableName parameter?

GhisLabo
GhisLabo Community Member Qubie ✭

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:

Tagged:

Best Answer

  • fleb
    fleb Czech RepublicCommunity Member Sage ✭✭✭
    Accepted Answer

    @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.

Answers

  • GhisLabo
    GhisLabo Community Member Qubie ✭

    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.

  • RDurant
    RDurant That's a vague questionCommunity Member Qubie ✭

    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?

  • fleb
    fleb Czech RepublicCommunity Member Sage ✭✭✭

    And what about sending your variables to the embedded data fields and then accessing them using piped text?

  • GhisLabo
    GhisLabo Community Member Qubie ✭
    edited December 2018

    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.