Multi-Instance JSON Collections in javascript using a script task

Hi everyone,

I’ve got the following problem:
I’m trying to implement a multi instance User task in which each user has to approve something.
I want to get every User ID of a certain group and then assign each user of that group a task.
To get the list of users I’ve used a script task with the following code in javascript:
var memberList = execution.processEngine.getIdentityService().createUserQuery().memberOfGroup('groupId').list();
Now I want to iterate over the given list and then put the result into a SPIN JSON so that I can use it as the collection for my Multi instance task.
What I have so far:
var jsonValue = S('{ "values":[] }');
var i;
for(i = 0; i<memberList.length;i++){
var id = memberList[i].getId();
jsonValue.prop("values").append(id); //Idea is to add ID of User to JSON - not sure if it works
Afterwards set is as a process variable:
execution.setVariable("memberIdList", jsonValue);

In my Multi instance User task I’ve set the collection to:
And for the element variable I’ve set it to:
Although I’m not sure what I have to put as the element variable in this case.

If I now start a request via postman I get the following response:
“type”: “ClassCastException”,
“message”: “class org.camunda.spin.impl.json.jackson.JacksonJsonNode cannot be cast to class java.lang.String (org.camunda.spin.impl.json.jackson.JacksonJsonNode is in unnamed module of loader @6477463f; java.lang.String is in module java.base of loader ‘bootstrap’)”

Where’s my mistake? I’ve already tried multiple ways to approach this like creating an object and then converting it but everything I’ve tried is not working.

Could you please help me with this?

Thank you!

Take a look at this thread:

It walks through how to use Multi-instance with arrays, and there are json examples. The example is for DMN, but it is the same configuration setup for your use case.

Also take a look at:

var arr = [ "hello", "world" ];
for each (a in arr) {

in the “For Each Expression” section. You can simplify your code using the Nashorn extensions.

In general it is must more clear and dev friendly to build your objects in their native forms (such as js) and then only convert to SPIN at the very end.

So you do not need to have var jsonValue = S('{ "values":[] }');. You can just create a js array, and build your list. When you do setVariable() then you can set your SPIN. Something like S(JSON.Stringify(jsonValue)) where jsonValue is a regular js array or object.

My recommendation is to rebuild from the examples above and the examples in teh DMN Looping link.