Local variable scope with parallel gateway

Let’s begin taking a look at this flow:

And then it’s subprocess:

Task C set a local variable named “myFavouriteColor”, and then get it in parallel Task D, parallel Task E and finally in task F. I have two big problems:

  1. Local variable “myFavouriteColor” is null in task F.
  2. Local variable “myFavouriteColor” is null in parallel task E, however it’s not in parallel task D…

My question is, how can I access my local variable the right way using a parallel gateway?

Also I attached the related process xml.

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:flowable="http://flowable.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.flowable.org/processdef">
  <process id="local_variable_scope" name="Local Variable Scope" isExecutable="true">
    <startEvent id="startEvent1" flowable:formFieldValidation="true"></startEvent>
    <scriptTask id="sid-8903327E-744E-4745-8B35-EB92A8A2FE2A" name="Task A" scriptFormat="JavaScript" flowable:autoStoreVariables="false">
      <script><![CDATA[var myList = new java.util.ArrayList();
myList.add("blue");
myList.add("yellow");
execution.setVariable("myList", myList);]]></script>
    </scriptTask>
    <sequenceFlow id="sid-AD11BA9D-2096-4361-AED7-87D46819682E" sourceRef="startEvent1" targetRef="sid-8903327E-744E-4745-8B35-EB92A8A2FE2A"></sequenceFlow>
    <scriptTask id="sid-6FCCF2EC-D1D9-427D-8810-FF0DEAD1BB6A" name="Task B" scriptFormat="JavaScript" flowable:autoStoreVariables="false">
      <script><![CDATA[print("End of execution");]]></script>
    </scriptTask>
    <endEvent id="sid-8EF4D1FC-42A4-4D95-B527-0F05AEBC7CFD"></endEvent>
    <subProcess id="sid-49ACD7F1-2199-4391-A328-9969EF91FD30" name="Subprocess">
      <multiInstanceLoopCharacteristics isSequential="false" flowable:collection="myList" flowable:elementVariable="myElement"></multiInstanceLoopCharacteristics>
      <startEvent id="sid-19DCD093-CF7B-489B-BB6C-55CE87F901C2" flowable:formFieldValidation="true"></startEvent>
      <scriptTask id="sid-B2025C7B-9AD7-4D55-B111-B8959F63142A" name="Task C" scriptFormat="JavaScript" flowable:autoStoreVariables="false">
        <script><![CDATA[var myElement_ = execution.getVariable("myElement");
print("Let's print my element of the list: " + myElement_)

print("Let's set a local variable with my favourite color: " + myElement_)
execution.setVariableLocal("myFavouriteColor", myElement_);]]></script>
      </scriptTask>
      <sequenceFlow id="sid-D7FE41EA-B3EE-43EC-AD0C-41A510908E5C" sourceRef="sid-19DCD093-CF7B-489B-BB6C-55CE87F901C2" targetRef="sid-B2025C7B-9AD7-4D55-B111-B8959F63142A"></sequenceFlow>
      <parallelGateway id="sid-D1E90BBC-075A-4DB0-83B5-2B6AE14C0E96"></parallelGateway>
      <sequenceFlow id="sid-BAA5E59A-9DD1-4203-B1C3-0A2418E39A9B" sourceRef="sid-B2025C7B-9AD7-4D55-B111-B8959F63142A" targetRef="sid-D1E90BBC-075A-4DB0-83B5-2B6AE14C0E96"></sequenceFlow>
      <scriptTask id="sid-18C8BB80-9617-4165-827D-E9BEB1006A61" name="Task D" scriptFormat="JavaScript" flowable:autoStoreVariables="false">
        <script><![CDATA[print("In parallel task D my favourite color is: " + execution.getVariableLocal("myFavouriteColor"));]]></script>
      </scriptTask>
      <scriptTask id="sid-7E5BAEB6-67FB-4950-B951-40B816E90D83" name="Task E" scriptFormat="JavaScript" flowable:autoStoreVariables="false">
        <script><![CDATA[print("In parallel task E my favourite color is: " + execution.getVariableLocal("myFavouriteColor"));]]></script>
      </scriptTask>
      <sequenceFlow id="sid-5A82F995-3837-4BE8-80B3-7D72098238DE" sourceRef="sid-D1E90BBC-075A-4DB0-83B5-2B6AE14C0E96" targetRef="sid-18C8BB80-9617-4165-827D-E9BEB1006A61"></sequenceFlow>
      <sequenceFlow id="sid-046EACA2-9BC2-490C-8FFB-8C59B6FA9829" sourceRef="sid-D1E90BBC-075A-4DB0-83B5-2B6AE14C0E96" targetRef="sid-7E5BAEB6-67FB-4950-B951-40B816E90D83"></sequenceFlow>
      <parallelGateway id="sid-DD358641-AA93-47A3-ACC1-5AAB2937583D"></parallelGateway>
      <sequenceFlow id="sid-B1BA5A5E-5435-4538-8A85-0C0AA8D8AF1C" sourceRef="sid-18C8BB80-9617-4165-827D-E9BEB1006A61" targetRef="sid-DD358641-AA93-47A3-ACC1-5AAB2937583D"></sequenceFlow>
      <sequenceFlow id="sid-7EA9C61E-7D97-43F8-8290-45695E11F001" sourceRef="sid-7E5BAEB6-67FB-4950-B951-40B816E90D83" targetRef="sid-DD358641-AA93-47A3-ACC1-5AAB2937583D"></sequenceFlow>
      <scriptTask id="sid-707BCD16-79E5-446D-87FA-13C9478F2588" name="Task F" scriptFormat="JavaScript" flowable:autoStoreVariables="false">
        <script><![CDATA[print("In task F my favourite color is: " + execution.getVariableLocal("myFavouriteColor"));]]></script>
      </scriptTask>
      <sequenceFlow id="sid-D7227DC5-2BA4-4209-852D-018589574AC8" sourceRef="sid-DD358641-AA93-47A3-ACC1-5AAB2937583D" targetRef="sid-707BCD16-79E5-446D-87FA-13C9478F2588"></sequenceFlow>
      <endEvent id="sid-F1A04FCC-090B-41A4-AFD0-7BD07ABE14A0"></endEvent>
      <sequenceFlow id="sid-0B93FC29-1137-45B0-95B9-5D7FE7C741A6" sourceRef="sid-707BCD16-79E5-446D-87FA-13C9478F2588" targetRef="sid-F1A04FCC-090B-41A4-AFD0-7BD07ABE14A0"></sequenceFlow>
    </subProcess>
    <sequenceFlow id="sid-67EB8F32-FD94-4599-B284-DF61EFDBE07E" sourceRef="sid-6FCCF2EC-D1D9-427D-8810-FF0DEAD1BB6A" targetRef="sid-8EF4D1FC-42A4-4D95-B527-0F05AEBC7CFD"></sequenceFlow>
    <sequenceFlow id="sid-7008F768-708E-49BC-A52D-91BDED3DD308" sourceRef="sid-8903327E-744E-4745-8B35-EB92A8A2FE2A" targetRef="sid-49ACD7F1-2199-4391-A328-9969EF91FD30"></sequenceFlow>
    <sequenceFlow id="sid-81A415D0-F4DD-41D2-B96D-62E042B08F36" sourceRef="sid-49ACD7F1-2199-4391-A328-9969EF91FD30" targetRef="sid-6FCCF2EC-D1D9-427D-8810-FF0DEAD1BB6A"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_local_variable_scope">
    <bpmndi:BPMNPlane bpmnElement="local_variable_scope" id="BPMNPlane_local_variable_scope">
      <bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
        <omgdc:Bounds height="30.0" width="30.0" x="100.0" y="163.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-8903327E-744E-4745-8B35-EB92A8A2FE2A" id="BPMNShape_sid-8903327E-744E-4745-8B35-EB92A8A2FE2A">
        <omgdc:Bounds height="80.0" width="100.0" x="214.0" y="138.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-6FCCF2EC-D1D9-427D-8810-FF0DEAD1BB6A" id="BPMNShape_sid-6FCCF2EC-D1D9-427D-8810-FF0DEAD1BB6A">
        <omgdc:Bounds height="80.0" width="100.0" x="585.0" y="135.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-8EF4D1FC-42A4-4D95-B527-0F05AEBC7CFD" id="BPMNShape_sid-8EF4D1FC-42A4-4D95-B527-0F05AEBC7CFD">
        <omgdc:Bounds height="28.0" width="28.0" x="750.0" y="161.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-49ACD7F1-2199-4391-A328-9969EF91FD30" id="BPMNShape_sid-49ACD7F1-2199-4391-A328-9969EF91FD30" isExpanded="false">
        <omgdc:Bounds height="80.0" width="100.0" x="390.0" y="137.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-AD11BA9D-2096-4361-AED7-87D46819682E" id="BPMNEdge_sid-AD11BA9D-2096-4361-AED7-87D46819682E">
        <omgdi:waypoint x="129.94999917679615" y="178.0"></omgdi:waypoint>
        <omgdi:waypoint x="213.99999999998346" y="178.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-81A415D0-F4DD-41D2-B96D-62E042B08F36" id="BPMNEdge_sid-81A415D0-F4DD-41D2-B96D-62E042B08F36">
        <omgdi:waypoint x="489.95000000000005" y="176.48717948717947"></omgdi:waypoint>
        <omgdi:waypoint x="584.9999999999984" y="175.51230769230764"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-67EB8F32-FD94-4599-B284-DF61EFDBE07E" id="BPMNEdge_sid-67EB8F32-FD94-4599-B284-DF61EFDBE07E">
        <omgdi:waypoint x="684.9499999999999" y="175.0"></omgdi:waypoint>
        <omgdi:waypoint x="750.0" y="175.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-7008F768-708E-49BC-A52D-91BDED3DD308" id="BPMNEdge_sid-7008F768-708E-49BC-A52D-91BDED3DD308">
        <omgdi:waypoint x="313.9499999999971" y="177.7159090909091"></omgdi:waypoint>
        <omgdi:waypoint x="390.0" y="177.2838068181818"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
  <bpmndi:BPMNDiagram id="BPMNDiagram_sid-49ACD7F1-2199-4391-A328-9969EF91FD30">
    <bpmndi:BPMNPlane bpmnElement="sid-49ACD7F1-2199-4391-A328-9969EF91FD30" id="BPMNPlane_sid-49ACD7F1-2199-4391-A328-9969EF91FD30">
      <bpmndi:BPMNShape bpmnElement="sid-19DCD093-CF7B-489B-BB6C-55CE87F901C2" id="BPMNShape_sid-19DCD093-CF7B-489B-BB6C-55CE87F901C2">
        <omgdc:Bounds height="30.0" width="30.0" x="60.0" y="223.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-B2025C7B-9AD7-4D55-B111-B8959F63142A" id="BPMNShape_sid-B2025C7B-9AD7-4D55-B111-B8959F63142A">
        <omgdc:Bounds height="80.0" width="100.0" x="162.0" y="198.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-D7FE41EA-B3EE-43EC-AD0C-41A510908E5C" id="BPMNEdge_sid-D7FE41EA-B3EE-43EC-AD0C-41A510908E5C">
        <omgdi:waypoint x="89.94999902632786" y="238.0"></omgdi:waypoint>
        <omgdi:waypoint x="161.99999999995723" y="238.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape bpmnElement="sid-D1E90BBC-075A-4DB0-83B5-2B6AE14C0E96" id="BPMNShape_sid-D1E90BBC-075A-4DB0-83B5-2B6AE14C0E96">
        <omgdc:Bounds height="40.0" width="40.0" x="361.0" y="218.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-BAA5E59A-9DD1-4203-B1C3-0A2418E39A9B" id="BPMNEdge_sid-BAA5E59A-9DD1-4203-B1C3-0A2418E39A9B">
        <omgdi:waypoint x="261.9499999999338" y="238.0"></omgdi:waypoint>
        <omgdi:waypoint x="361.0" y="238.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape bpmnElement="sid-18C8BB80-9617-4165-827D-E9BEB1006A61" id="BPMNShape_sid-18C8BB80-9617-4165-827D-E9BEB1006A61">
        <omgdc:Bounds height="80.0" width="100.0" x="465.0" y="90.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-7E5BAEB6-67FB-4950-B951-40B816E90D83" id="BPMNShape_sid-7E5BAEB6-67FB-4950-B951-40B816E90D83">
        <omgdc:Bounds height="80.0" width="100.0" x="465.0" y="283.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-5A82F995-3837-4BE8-80B3-7D72098238DE" id="BPMNEdge_sid-5A82F995-3837-4BE8-80B3-7D72098238DE">
        <omgdi:waypoint x="381.5" y="218.5"></omgdi:waypoint>
        <omgdi:waypoint x="381.5" y="130.0"></omgdi:waypoint>
        <omgdi:waypoint x="465.0" y="130.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-046EACA2-9BC2-490C-8FFB-8C59B6FA9829" id="BPMNEdge_sid-046EACA2-9BC2-490C-8FFB-8C59B6FA9829">
        <omgdi:waypoint x="381.49999999999994" y="257.4388002364066"></omgdi:waypoint>
        <omgdi:waypoint x="381.5" y="323.0"></omgdi:waypoint>
        <omgdi:waypoint x="465.0" y="323.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape bpmnElement="sid-DD358641-AA93-47A3-ACC1-5AAB2937583D" id="BPMNShape_sid-DD358641-AA93-47A3-ACC1-5AAB2937583D">
        <omgdc:Bounds height="40.0" width="40.0" x="630.0" y="218.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-B1BA5A5E-5435-4538-8A85-0C0AA8D8AF1C" id="BPMNEdge_sid-B1BA5A5E-5435-4538-8A85-0C0AA8D8AF1C">
        <omgdi:waypoint x="564.9499999999526" y="130.0"></omgdi:waypoint>
        <omgdi:waypoint x="650.0" y="130.0"></omgdi:waypoint>
        <omgdi:waypoint x="650.0" y="218.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-7EA9C61E-7D97-43F8-8290-45695E11F001" id="BPMNEdge_sid-7EA9C61E-7D97-43F8-8290-45695E11F001">
        <omgdi:waypoint x="564.9499999998759" y="323.0"></omgdi:waypoint>
        <omgdi:waypoint x="650.0" y="323.0"></omgdi:waypoint>
        <omgdi:waypoint x="650.0" y="257.91169022379273"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape bpmnElement="sid-707BCD16-79E5-446D-87FA-13C9478F2588" id="BPMNShape_sid-707BCD16-79E5-446D-87FA-13C9478F2588">
        <omgdc:Bounds height="80.0" width="100.0" x="765.0" y="198.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-D7227DC5-2BA4-4209-852D-018589574AC8" id="BPMNEdge_sid-D7227DC5-2BA4-4209-852D-018589574AC8">
        <omgdi:waypoint x="669.5019652650799" y="238.44207317073173"></omgdi:waypoint>
        <omgdi:waypoint x="764.9999999999886" y="238.1518237082067"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape bpmnElement="sid-F1A04FCC-090B-41A4-AFD0-7BD07ABE14A0" id="BPMNShape_sid-F1A04FCC-090B-41A4-AFD0-7BD07ABE14A0">
        <omgdc:Bounds height="28.0" width="28.0" x="910.0" y="224.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-0B93FC29-1137-45B0-95B9-5D7FE7C741A6" id="BPMNEdge_sid-0B93FC29-1137-45B0-95B9-5D7FE7C741A6">
        <omgdi:waypoint x="864.9499999999999" y="238.0"></omgdi:waypoint>
        <omgdi:waypoint x="910.0" y="238.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

Any suggestions? Someone?

1 Like

Ummm, not sure the gif was necessary. A quick search brings the following thread that may help:

Cheers
Paul.

Hi @PHH! I read that the workaround is “[…] use the parallel gateway inside an embedded sub process, you know there’s a parent sub process execution on which you can set local variables and that are accessible for every child execution inside the embedded sub process.”, however I tried this and it’s not working. I’m doing something wrong or I’m misunderstanding the workaround?

As you can see below, I wrapped all the tasks and gateways inside an embedded sub-process and “Task C” set the local variable:

But my output is the same:

Task C; execution id: c8f3d0de-7914-11ea-8398-4a868ad9bccc
Let’s print my element of the list: blue
Let’s set a variable with my favourite color: blue
Task D; execution id: c8f3d0de-7914-11ea-8398-4a868ad9bccc
In parallel task D my favourite color is: blue
In parallel task D my favourite color is (local): blue
Task E; execution id: c8f61ad5-7914-11ea-8398-4a868ad9bccc
In parallel task E my favourite color is: null
In parallel task E my favourite color is (local): null
Task F; execution id: c8f61ad5-7914-11ea-8398-4a868ad9bccc
In task F my favourite color is: null
In task F my favourite color is (local): null
Task C; execution id: c903609c-7914-11ea-8398-4a868ad9bccc
Let’s print my element of the list: yellow
Let’s set a variable with my favourite color: yellow
Task D; execution id: c903609c-7914-11ea-8398-4a868ad9bccc
In parallel task D my favourite color is: yellow
In parallel task D my favourite color is (local): yellow
Task E; execution id: c90646d3-7914-11ea-8398-4a868ad9bccc
In parallel task E my favourite color is: null
In parallel task E my favourite color is (local): null
Task F; execution id: c90646d3-7914-11ea-8398-4a868ad9bccc
In task F my favourite color is: null
In task F my favourite color is (local): null
In final task B my favourite color is: null
End of execution

Setting variables on executions locally will keep those variables around until the execution are cleaned up. This can happen in a variety of places, e.g. because the engine decides to merge executions due to parallel paths being merged. In the example here, this is (most likely) what is happening: at point F, the previous executions are merged.

In your example, you’re saying you use a subproces. But do you also store the variables on the execution related to the subprocess?

An alternative could be store store the variables on the process instance level, but make them unique, see Multi-Instance and then waiting for the events for a post where this was done.

Hi @joram! What I’ve done is setting a local variabe in “Task C”, with execution.setVariableLocal(key, value) method. Is that the way I should achieve that workaround? Or I’m a little confused and I should store the local variable in some other way?

If you set the variable local on task C, it will be stored in the execution that is at that moment acttive in that step. However, that execution will split into two later due to the parallel gateway. Hence why it doesn’t work - those variables don’t get propagated when split.

To solve it, you can either

  • look up the first ‘common parent’ execution, in this situation the subprocess execution. On that common execution, the variables can be stored and they’re accessible for all child executions.
  • Make the variable name unique (as done in the referenced link)