Question T13692
Visible to All Users

DefaultValueExpression - Multi-Select

created 2 years ago

Is it possible to prefill a multi-select dropdown using a default expression?

It seems to want to accept an array of strings, but returning an array of strings from a custom function does not populate the field with choices.

Example JSON:
Screenshot 2023-06-28 at 4.05.24 PM.png

I can confirm that the custom function does return a proper JSON array of strings, I've tried passing back a comma-separated string list as well which did not work either.

Any way around this? The values to be populated are dynamically generated as the user goes through the survey, ie: from other questions, so they can't be passed in prior.

Example function:
Screenshot 2023-06-28 at 4.11.02 PM.png

Comments (1)

    Can also confirm that the value IS being set. Just not displaying in the dropdown.
    Screenshot 2023-06-28 at 4.24.02 PM.png

    Answers approved by surveyjs Support

    created 2 years ago

    Hello,
    Is "getRelationshipsList" async?
    How do you get choices? Do you set choices to this question from the code?

    Thank you,
    Andrew
    SurveyJS Team

      Comments (3)

        Hi Andrew,

        It's not async, it just accepts some matrix arrays from the survey, processes them into strings, and returns them as an array.

        I guess that's my question, can dropdown choices be set dynamically using an expression?

          Basically, the choices of the question should be responses from further up in the survey. In this case, they are Dynamic matrix fields. I guess I was assuming that passing in values would dynamically create those choices.

            Hello,
            There are several ways to implement the needed functionality. Probably the easiest one is to have the same defaultValueExpression, but change your "getRelationshipsList". Since the function is call inside a question then you can access the question in the function context as: this.question and you can always access survey as this.survey.
            You can set up the question choices as if(this.question) this.question.choices = yourFuncResult;.
            Theoretically, by adding this line of code before returning the value you should solve the issue.

            Thank you,
            Andrew
            SurveyJS Team