Need process variable accessible from anywhere in a nested root level process

Say that I have a highly nested process instance, with nesting created by the use of call activities. In the root level process instance I have a process variable defined. From a task listener on a task no matter how deep in the hierarchy , I want to be able to access the value stored in the root level process instance.

How do I do this? I don’t think there is a Java API method that will return the root level process instance is there?

If not, it seems like I must get the process instance of the task associated with the task listener, and see if it has a parent process instance. If there is a parent process instance, I must get the parent process for it, and so on until a process instance doesn’t have a parent process instance. At that point, I should be able to retrieve the sought after process instance variable.

Would this approach work? Is there a better way?

What I really want it seems, is a global process variable accessible from anywhere in a process instance hierarchy. From what I’ve learned, however, this sort of global variable doesn’t exist.

Any help is much appreciated.

Would a Parse Listener that ensures that every call activity has ‘variable in: “All”’ and ‘variable out: “All”’ added work?

That way when your called process starts, it automatically inherits the variables of the parent process, and hand any variables it created back to the parent process… Downside is that they get set when the called process is called. So if another activity in the process changes them while it’s running, it would work on the old values.

Walking your process tree looking for all the parents and collecting all the current values of variables would obviously work too. I just don’t know what that would do performance.

This is a promising idea. First, I wouldn’t have to specify “All” for both in and out mappings would I? I could just map the one variable that I want passed in to every scope. Secondly, for my needs, once the value is set in the top level process, it would never would get modified, just read. Thanks for the suggestion. I might go with it.

Hi @cr.irvine,

you can cast the execution to an ExecutionEntity. Here you can access the root process instnace id.

Disclaimer: It’s internal api and may change in the future.

This is an example to access a variable from the root process:

import org.camunda.bpm.engine.delegate.DelegateTask;
import org.camunda.bpm.engine.delegate.TaskListener;
import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity;

public class TaskiListenerExample implements TaskListener {

  @Override
  public void notify(DelegateTask delegateTask) {
    ExecutionEntity execution = (ExecutionEntity) delegateTask.getExecution();
    String rootProcessInstanceId = execution.getRootProcessInstanceId();
    Object variable = execution.getProcessEngineServices().getRuntimeService().getVariable(rootProcessInstanceId,
        "myvar");
  }
}

Benefit: No need to copy this variable between call activity.

Hope this helps, Ingo

1 Like

@Ingo_Richtsmeier Thanks so much. It helps phenomenally, actually. I wonder if the use case is common enough to make this a supported API.

I only really suggested it because of your other thread about the difference between a parse listener and an execution listener.

Glad that Ingo could come up with something that’s a little more supportable.

I think it was a pretty good suggestion.