Get process instance business key from Delegate Task

I am trying to retrieve business key for a process instance of a task through task listeners. The event type of my listener is create. Below is my implementation of task listener:

public void notify(DelegateTask delegateTask) {
    LOGGER.info(delegateTask.getProcessInstanceId()); // Prints correct ID
    ProcessInstance instance =
    Context.getProcessEngineConfiguration()
            .getRuntimeService()
            .createProcessInstanceQuery()
            .processInstanceId(delegateTask.getProcessInstanceId())
            .singleResult(); // instance is null
    LOGGER.info("Business key: {}", instance.getBusinessKey()); // NPE occurs here
}

But I keep getting NPE in above code. Are create listeners executed before the creation of process instance?

Hi @shan-96,

delegateTask.getExecution().getProcessInstance().getBusinessKey()

will do the trick.

Hope this helps, Ingo

5 Likes

Wow thanks @Ingo_Richtsmeier ! Getting the business key was much easier than I thought. This works.

Hi @shan-96 , you can try like this also:

delegateTask.getExecution().getProcessBusinessKey();
1 Like

Thanks @aravindhrs and @Ingo_Richtsmeier. Is it guaranteed though that results from both APIs will be consistent?

Hi @shan-96,

they are: camunda-bpm-platform/engine/src/main/java/org/camunda/bpm/engine/impl/pvm/runtime/ExecutionImpl.java at master · camunda/camunda-bpm-platform · GitHub

Hope this helps, Ingo

1 Like