Form dropdown using JSON as source

Hi

I have an HTTP process which retrieves the following JSON:
[{“account”:“1234567890”},{“account”:“1234567899”},{“account”:“1234567888”},{“account”:“1234567999”},{“account”:“1234567444”}]

I would like to list these accounts in a Dropdown on a form. Any idea what expression I should use as nothing seems to work.

Thanks

Hey.

Here’s a way I handle these type of cases.

In your process flow, after the Http Task add a Script Task with Groovy language. Then, try the following code:

//retrieve JSON response
final def response = execution.getVariable("yourResponseVariableWithJson");

//response must not be null and empty
if (response != null && response.toString().length() > 2) {

    //array to store options in appropriate format
    final def String options = "[";

    //loop through JSON array
    response.each {
        //from each iterated object in array, get the account variable and add it to the options
        options += '{"id":"' + it.account + '","name":"' + it.account + '"},';
    }
     
    //remove trailing comma
    options = options.substring(0, options.length() - 1);

    //close the array
    options += "]";

    //store options as a process variable
    execution.setVariable("myDropDownOptions", options);
} else {
    execution.setVariable("myDropDownOptions", "");
}

Then, you can reference the stored variable like this:

1 Like

That’s great. Thank you.

1 Like

Hi, do you have the groovy to get the selected value from the Dropdown after the form has been completed?

If the ID of the Dropdown is selectedatlasloanaccount, would it be…

final def response = execution.getVariable(“selectedatlasloanaccount”);?

Hey.

That is correct.

All form values are submitted as process variables with the respective ID, hence the given snippet is enough to get the selected value.

Hi, I have the same problem and I am can see my variable is in the below format

{
“name”: “myDropDownOptions”,
“type”: “string”,
“value”: “[{“id”:“11”,“name”:“Option 1”},{“id”:“22”,“name”:“Option 2”},{“id”:“33”,“name”:“Option 3”}]”,
“scope”: “local”
},

but the execution of the model is failing with below error
is the a specific data type needed for this variable other than string, if so how to declare it

org.flowable.common.engine.api.FlowableException: Error getting value for optionsExpression: ${myDropDownOptions}