cancel
Showing results for 
Search instead for 
Did you mean: 

Access parent ProcessInstance in execution listener

jakub_telicki
Champ in-the-making
Champ in-the-making
I am using ExecutionListener attached on the start event at the Process level through customized parser - I am adding extra logic to process creation.
When I create process through 'Call Activity' I want to get BusinessKey of parent.

Simplified code accessing parent might look like that:

        public void notify(DelegateExecution process) {
            String parentId = process.getParentId();//returuns null
            ProcessInstance parent = process.getEngineServices().getRuntimeService().createProcessInstanceQuery()
                    .processInstanceId(parentId).singleResult();           
            System.out.println(parent.getBusinessKey());
        }


But the "parentId" is not available, moreover having the parentId is still not sufficient to get the parent instance, the parent process is not visible (not saved yet I guess), code like that fails as well:

        public void notify(DelegateExecution process) {
            String parentId = ((ExecutionEntity)process).getSuperExecution().getProcessInstanceId();    
            ProcessInstance parent = process.getEngineServices().getRuntimeService().createProcessInstanceQuery()
                    .processInstanceId(parentId).singleResult();//returns null      
            System.out.println(parent.getBusinessKey());
        }


Code that works looks like that:

        public void notify(DelegateExecution process) {
            ExecutionEntity parent = ((ExecutionEntity)process).getSuperExecution();                     
            System.out.println(parent.getBusinessKey());//success
        }


But according to User Guide classes such as ExecutionEntity shall not be use as they may change in future releases.

Is there a way to get the ProcessInstance/BusinessKey of the parent process, from withing start execution listener of subprocess created through call activity?


1 REPLY 1

trademak
Star Contributor
Star Contributor
When you add an execution listener to the start event in a sub process, the sub process instance will not be persisted yet. It will be persisted when the first wait state is reached. So it might be a good idea to add a service task or even a manual task with async is true and move the execution listener to that task. Then it's possible to do queries. What you could use is the ProcessInstanceQuery subProcessInstanceId method and set the process instance id of the sub process there. That should give back the parent process.

Best regards,