How to return a collection to be used in a multi instance sub process from an external task service?

I am implementing a sequential multi instance sub process similar to the one implemented in this thread.

In that thread @StephenOTT creates a collection assigned to the variable orders in a groovy script and then sets that orders variable to the collection property of the multi instance configuration on the sub process.

I need to be able to create the collection from the results returned by an external task service.

I currently use the rest api to complete the external task but I currently only set a string variable in the body of the complete action.

I am not using java for my external service task but can easily serialize things to JSON or a JSON array if that would help accomplish this.

How do I define the properties value, type, and valueInfo to return a collection from an external service task where the external service task is not written in java so that I can use the returned variable as the collection property in the multi instance configuration of a subprocess?

2 Likes

@Chris_Magnuson when you “complete” the external task you should be able to define Variable info in the body of the Complete request
https://docs.camunda.org/manual/7.5/reference/rest/external-task/post-complete/#request-body

So when you complete the external task this means whatever the External service was doing is complete and you can pass the array/collection of data through the complete api call

@StephenOTT Based on the Supported Variable Values documentation I don’t see a type for array/collection.

What does passing an array/collection look like in terms of the value, type, and valueInfo properties available for a variable you return when you do a post against the rest api to complete a process?

Return JSON with an array

@StephenOTT Will I have to process that array with spin to turn it into something else for it to be usable as the collections property or can I just assign that variable that will be of type JSON that happens to be an array and the system will just sort it out?

1 Like

When you want to access the data inside of the array/json you would use the S() / spin framework to get .elements(), etc.

But my understanding is that when you pass the json in through and make it type json you should be good to go. (have not tested to be 100%)

I will test it out and reply back, thanks for your help.

@Chris_Magnuson this is what i was looking for:

camunda:collection=“${variable.elements()}”

So you should be good to go once you bring in the JSON on External Task completion

@Chris_Magnuson I tested for you and it works.

go with something like {"customer": ["Kermit", "Waldo"]}

and then in your collection do:
${variableName.prop("customer").elements()}. Note: if you don’t have the .elements() it does not work.

The VariableName is your JSON from the external task, and would be Type: JSON.

Chris, did this work out for you?

@StephenOTT Still working on it, just got the code done yesterday that will now properly create a camunda variable of the type JSON for a property of an object I am returning if it doesn’t match one of the standard camunda primitive types.

Haven’t used it yet for elements collection of a multi instnace subprocess but am getting close.

What I am trying at the moment is to have an output variable named Environments from an external service task where the type is script, script format groovy, and script is ${Response.prop("Environment").elements()}.

I am hopeful that will mean that I can then just use the variable Environments as the multi instance subprocess collection value.

Will reply back once I find out.

Thanks,

Chris

Hi @StephenOTT @Chris_Magnuson I’m facing a similar problem, I call a rest api and get a Json which has list of objects, from there i’m forming a json list as you mentioned above
list = {“numbers”: [12345, 23456]}
so if I pass ${list.prop(“numbers”).elements()} in collection it should work ??. correct me if I’m wrong