How to access a camunda collection in javascript?

Hello,

In my camunda workflow, there is a camunda output parameter defined as a list.I would like to know

  1. How a list mentioned as camunda:resultVariable can be accessed in output parameter?
  2. If a variable value is set using execution.setVariable(…) , how to access it in camunda output parameter which has inline java script (<camunda:outputParameter name="DirectToList">
    in the code below) ?
  3. When checking typeof List1 , it is showing as an object instead of array. In camunda javascript, how to cast object as array? Object.keys(List1) is throwing error.
<camunda:outputParameter name="List1">
            <camunda:list />
<bpmn:scriptTask id="Activity_0uomvx7" name="Iterate" scriptFormat="javascript" 
camunda:resultVariable="List1">
      <bpmn:incoming>Flow_11fy66f</bpmn:incoming>
      <bpmn:outgoing>Flow_1gaa7g0</bpmn:outgoing>
      <bpmn:script>
	  
	  var List2 = execution.getVariableTyped("List1", true).getValue();

	  var arry1 = JSON.parse(originalList)

	  for (var i = 0; i &lt;  arry1.length; i++) {
		List2.add(arry1[i].name)
	  }
	  execution.setVariable("IsMyresultsSelected", List2.length&gt;0);
	  List2
	  </bpmn:script>
</bpmn:scriptTask>
<camunda:outputParameter name="DirectToList">
	<camunda:script scriptFormat="javascript">
	
	<<How to access "IsMyresultsSelected" variable here >> ?
	
	just using IsMyresultsSelected here is returning empty.
	
	or
	
	<<How to iterate through camunda result variable List1 here and find the length? The below code throws error>> 
	  
	  var t1 = JSON.stringify(List1);
	  var t2 = JSON.parse(t1);-->
	  t2.length;
	</camunda:script>
</camunda:outputParameter>

Hello @Shia!

Assuming that I understand your process descriptions right, I created a model
list.bpmn (4.1 KB).

As far as I understand your script you won’t be able to access List1 as the result variable until the script of Activity_0uomvx7 has been processed. So you would have to have a created List1 variable via an input-mapping of the script task or created by a previous task, like in my provided model during Create List1.

With execution.setVariable("IsMyresultsSelected", List2.length < 0); you are setting the variable to the global scope of the process, meaning you should be able to access it right away in the output-mapping by it’s name IsMyresultsSelected. The same is for the updated result of List1 and it’s length with List1.length.

Please see Process Variables | docs.camunda.org for general information of accessing variables.

What bpm-platform version you are running your process-model on?

Hope it helps!

1 Like

Good example :+1: Thanks!
A modified version of your model list.bpmn for possibly better understanding by others.
How-to access a camunda collection in javascript.bpmn (5.5 KB)

BTW, I’m using camunda-bpm-run-7.20.0

1 Like