Search code examples
bpmncamunda

Setting an execution's local variable according to the flow led to the sub-process


I have a BPMN process with a sub-process within it. There are different flows leading to the sub-process. And I want to know, once inside the sub-process' execution, which flow has led to the current execution.

To this end I think variables could be handy. So I conducted a test in which I wrote a couple of scripts for flow's listener leading to the sup-process.

execution.setVariableLocal("V", "Expecting it to be local to the sub-process' execution");

But it turned out that execution points to the outer/parent process and thus the variable was set in parent scope.

So is there anyway to set an execution local variable from outside?


Solution

  • The simplest approach (basically use getVariable instead of getVariableLocal):

    1. Add an execution listener to the take event of the sequence flows of interest
    2. In the execution listener, perform

      execution.setVariableLocal("flowTaken", execution.getCurrentTransitionId());
      
    3. Access it in the sub process via

      execution.getVariable("flowTaken");
      

    If it has to be a local variable in the sub process:

    1. Add an execution listener to the take event of the sequence flows of interest
    2. In the execution listener, perform

      execution.setVariableLocal("flowTaken", execution.getCurrentTransitionId());
      
    3. In the BPMN 2.0 XML, define a variable input mapping for the subprocess:

      <subProcess ...>
        <extensionElements>
          <camunda:inputOutput>
            <camunda:inputParameter name="flowTakenAsSubprocessLocalVariable">${flowTaken}</camunda:inputParameter>
          </camunda:inputOutput>
        </extensionElements>
        ...
      </subProcess>