How to pass variable from subprocess before it ends


#1

I have a main process, which has a lot of subprocesses and I need to periodically check if some condition is met(the task expires). The process looks like this:

image

The problem is that the process variable which is needed in the Check expiration task is created at the start of Activity 1(and never changed after that). So until the Activity 1 is finished, I don’t have access to that variable. Is there some way to pass variable to parent process before the subprocess terminates? I can think of a few solution, but like none of them:

  1. When the variable is set in subprocess, send it via signal/escalation to the parent and handle it there.
  • this will clutter the diagram with non-business workaround
  1. Copy the timer part of the process to Activity 1
  • besides not being fan of copy-pasting, this will also cause the handling code to be executed twice as often when in Activity 1.
  1. Extract the tasks which sets the variable to the parent process
  • This looks like a good solution, but I’m reusing the Activity 1 in a different process so I want it to stay self-contained. I would have to add the extracted tasks also to other processes.
  1. Retrieve it via some services in code(runtimeService.createVariableInstanceQuery maybe?)
  • This looks like a best approach, but I’m not sure how to do that in most efficient manner. I think the context of the subprocess won’t be loaded in the thread, right?
    I’ve tried to get to the subprocess somehow through DelegateExecution which is passed to my delegate, but couldn’t figure it out

Are there any other options or maybe a different way to model this?


#2

When you create the variable in Activity 1’s scope, you can access/determine what the parent process instance Id is through the execution.getParentProcessInstance() (or something close to that). Use that to navigate the hierarchy to your root scope and create the variable in that process instance.


#3

Hi, sorry for replying so late.
I’ve tried to get a root process from inside the Activity, but apparently that doesn’t work because it’s technically completely different process. So I’ve ended up searching for related processes using businessKey

public void setVariableByBusinessKey(final String businessKey, final String processVariable, final Object value){
	final List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey(businessKey).list();
	for (final ProcessInstance processInstance: processInstances){
		runtimeService.setVariable(processInstance.getId(), processVariable, value);
	}
}

This solves my problem