How To Move from a Task in One Sub process to a Task in Another

We have a use case where we need to move from one task in subproc2 to another task in subproc1. We have decided to use createProcessInstanceModification() Java API to achieve that. But it doesn’t seem to work.

Can you please let us know if we did anything wrong?

I have a main process like below:
image

In Sub Proc1, we have:
image

In Sub Proc2, we have
image

In Sub Proc2 Task 2, we have implemented a Java Delegate to move from Sub Proc 2 to Sub Proc 1:

runtimeService.createProcessInstanceModification(parentPid)
.cancelAllForActivity(subProc2-DefId)
.startBeforeActivity(subProc1-DefId)
.execute();

However, Task 2 in Sub Proc 2 is succeeded, but it didn’t move from Sub Proc 2 to Sub Proc 1, as expected. Essentially, the token stays with Sub Proc 2.

Thank you in advance for your help.

Hi,

rather than use the modification API, have you considered modelling this behaviour in the process model itself?

regards

Rob

Hi Rob
Thank you for the quick reply.
Yes, initially we tried to model it by using escalation event. Later we realized that we need to move to a specific task, say, Task 2 in Sub Process 1. As such, that approach won’t work for us.

Any other modeling technique can you share? Also, we’d like to know what’s the reason modification is not working.

Thanks
Jason

Hi @jchen5580,

don’t do process instance modification in the Java Delgate. Here is a quote from the docs:

Process instance modification within the same instance is not recommended! An activity which tries to modify its own process instance can cause undefined behavior, which should be avoided.

You can add a gateway and skip the first tasks in the the first subprocess when you come back from the second subprocess.

Hope this helps, Ingo

Hi Ingo,
Thank you for pointing out the constraint of the API. However, what you suggested to add a gateway is something we tried to avoid to keep the model clean.

I have made the following updates:
Sub Proc 2:

[Escalation is thrown after Task 1 is completed]

Main Process:


[ I made an event sub process and started with an Escalation Start task to capture the exact same event Sub Proc 2 throws]

Then I have the following code in Mod Task:
This is a two-step process in Delegate’s execute() method:
Step 1: move to Sub Proc 1

        execution.getProcessEngineServices().getRuntimeService()
                .createProcessInstanceModification(execution.getProcessInstanceId())
                .startBeforeActivity("subprod1Id")
                .cancelAllForActivity(execution.getActivityInstanceId()).execute();

This steps works only after Delegate’s execute() is completed.

Step 2: move to Task 2 of Sub Proc 1

             // get the active subprocess, which is expected to be Sub Proc 1
                ProcessInstance subprocess = execution.getProcessEngineServices().getRuntimeService()
                .createProcessInstanceQuery()
                .superProcessInstanceId(execution.getProcessInstanceId()).singleResult();

execution.getProcessEngineServices().getRuntimeService()
.createProcessInstanceModification(subprocess.getId())
.startBeforeActivity(“sp1.task2”).execute();

This step is failed because the first step hasn’t actually happened. As such, the subprocess is always Null.

We tried to ‘Asynchronous Before’ to manipulate the transaction boundary, but with no luck. Any suggestion that we can somehow get the the Sub Proc 1’s PID in step 2 prior to execute() completion?

Thank you for the help.

Jason