Access List process variable in a (Java)Script

Hello,

I would like to access a List process variable inside a script to add a new element.

But I get a stacktrace:

10:14:01.299 [main] ERROR org.camunda.bpm.engine.context - ENGINE-16004 Exception while closing command context: Unable to evaluate script: TypeError: scriptList.push is not a function in <eval> at line number 5
org.camunda.bpm.engine.ScriptEvaluationException: Unable to evaluate script: TypeError: scriptList.push is not a function in <eval> at line number 5
    at org.camunda.bpm.engine.impl.scripting.SourceExecutableScript.evaluateScript(SourceExecutableScript.java:119) ~[camunda-engine-7.4.0.jar:7.4.0]
    at org.camunda.bpm.engine.impl.scripting.SourceExecutableScript.evaluate(SourceExecutableScript.java:60) ~[camunda-engine-7.4.0.jar:7.4.0]
...

This is my script from a outputMapping:

      <bpmn:businessRuleTask id="BusinessRuleTask_0q7nf5e" name="make single decision" camunda:resultVariable="decisionResultBPMN" camunda:decisionRef="example-decision" camunda:mapDecisionResult="singleEntry">
        <bpmn:extensionElements>
          <camunda:inputOutput>
            <camunda:outputParameter name="resultList">
              <camunda:script scriptFormat="JavaScript"><![CDATA[print(decisionResultBPMN);
var scriptList = resultList;
print(scriptList);
print(scriptList instanceof Array);
scriptList.push(decisionResultBPMN);
print(scriptList);
scriptList;]]></camunda:script>
            </camunda:outputParameter>
          </camunda:inputOutput>
          <camunda:executionListener event="start">
            <camunda:script scriptFormat="JavaScript">print(inputValue);</camunda:script>
          </camunda:executionListener>
        </bpmn:extensionElements>
        <bpmn:incoming>SequenceFlow_0rigebs</bpmn:incoming>
        <bpmn:outgoing>SequenceFlow_1e6k5iz</bpmn:outgoing>
      </bpmn:businessRuleTask>

And this is the definition of resultList:

    List<Integer> inputList = Arrays.asList(new Integer[] {1, 3, 2});
    List<Integer> resultList = Arrays.asList(new Integer[] {90, 80});
    
    
    ProcessInstance processInstance = runtimeService().startProcessInstanceByKey(PROCESS_DEFINITION_KEY, 
        withVariables("inputList", inputList,
            "resultList", resultList));

Here is the output from the console:

1
10
[90, 80]
false
10:14:01.290 [main] ERROR org.camunda.bpm.engine.context - ENGINE-16006 BPMN Stack Trace:
    BusinessRuleTask_0q7nf5e (transition-destroy-scope, Execution[40])

How can I access a list process variable in a (Java)Script snippet.

Cheers, Ingo

Hi Ingo,

resultList is a java.util.List and due to the assignment var scriptList = resultList, scriptList is one as well and not a Javascript array. That is why I think you have to use #add instead of #push to add an element to the list.

Cheers,
Thorben

Hello,

I think that this post is related to my case but I can not find the solution to my issue.

I am trying to parse from a DMN result (resultVariable: result) the “group” column.

For example, assuming that the result of the DMN table is the following (two matching rules):

[{"assignee":"tomasM","group":"hr1"},{"assignee":"harris.smith","group":"hr2"}]

How could I, in the “Input/Output” tab of the business rule task, create an outputVariable “candidateGroup” that will look like: [hr1,hr2].

I need to pass later this variable “candidateGroup” inside my user task on “candidate group” field.

Something like the following, as javascript code, inside the outputParameter script does not work:

var system = java.lang.System;
system.out.println(result);
var candidateGroup=[];

for(var i in result){
   system.out.println(result[i].group.toString());
   candidateGroup.push(result[i].group.toString());
   system.out.println("group added");
}

To sum up, I want inside the script to make the candidateGroup a variable that contains the possible groups that a following task can be assigned to.

Any ideas are welcomed.