Get Variable value from Parent in Called Activity

I am a little confused about the syntax for referencing a variable that exists in the Parent process of a Called Activity. I realize I can pass the variables in when the Called Activity starts, but the variable may change and I want to be able to reference the current value of the variable from within my sub-process.

I am thinking it should be something like this:

execution.getProcessEngineServices().getRuntimeService().getRootProcessInstanceId().getVariable(processVariable)

Hi @Bill_Powell,

To get the calling process instance from the called one, try below code

execution.getSuperExecution().getProcessInstance();

And below code to get the variable value of the calling process instance

execution.getSuperExecution().getProcessInstance().getVariable([variableName]);

Thank you, Hassan, that was exactly what I needed.

I am seeing getSuperExecution return null when called inside of a Called Activity. I have attached the sub-process (sheduling.bpmn and the parent process Appraisal Overall.bpmn) . The script line in an intermediate conditional catch event is:

execution.getSuperExecution().getProcessInstance().getVariable('orderStatus').toLowerCase() == 'scheduled'

The error from Camunda is:

09-Aug-2019 23:07:29.519 SEVERE [http-nio-8080-exec-2] org.camunda.commons.logging.BaseLogger.logError ENGINE-16004 Exception while closing command context: Unable to evaluate script while executing activity 'ExclusiveGateway_1ppizw2' in the process definition with id 'Scheduling:1:8ea9b3f4-baf9-11e9-999a-0242c0a84201': java.lang.NullPointerException: Cannot invoke method getProcessInstance() on null object 

This same command works within another sub-process inside of a conditional start event for an event sub-process within the Called Activity. I don’t understand how getSuperExectuion could be null. The error occurs when the External Service tries to return.
Appraisal Overall.bpmn (29.2 KB)
Scheduling.bpmn (21.5 KB)

Hi @Bill_Powell

Based on the documentation of getSuperExecution() method, the method returns a value only in case the execution is a process instance execution.

But each outgoing sequence flow of the gateway represents new execution which is not a process instance execution.

In case this delegate execution is the process instance execution and this process instance was started by a call activity, this method returns the execution which executed the call activity in the super process instance.

So try the following to get it works in all cases

execution.getProcessInstance().getSuperExecution().getVariable('orderStatus').toLowerCase() == 'scheduled'

Instead of

execution.getSuperExecution().getProcessInstance().getVariable('orderStatus').toLowerCase() == 'scheduled'

1 Like

Thank you, Hassan. We have abandoned our initial idea of maintaining variables only in the parent process, since that stops the system from re-evaluating conditions in the sub-process because no variables are changing. We were hoping to avoid copying variable updates to both Parent and Sub-process. We have switched to only updating the sub-process, by searching for leafProcessInstances() and relying on the built-in capability for input/output of variables from a sub-process.