Evaluate businessKey from Task and CaseExecution after queries

Hi Team,

is there a easy evaluation of the businessKey from a task or caseExecution object after queries?

Current I use following implementation to evaluate the businessKey for org.camunda.bpm.engine.task.Task:
List<Task> tasks = taskService.createTaskQuery() .processDefinitionKey(processDefinitionKey) .initializeFormKeys() .orderByDueDate().asc() .orderByTaskCreateTime().asc().list(); ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(tasks.get(0).getProcessInstanceId()).singleResult(); processInstance.getBusinessKey();

I tried the same implementation for CaseExecution, but no processInstance object is returned.
List<CaseExecution> cases = processEngine.getCaseService().createCaseExecutionQuery() .caseDefinitionKey(processDefinitionKey) .active() .list(); ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().caseInstanceId(cases.get(0).getCaseInstanceId()).singleResult();

Thanks a lot for your help!

Hi Bernd,

What exactly do you want to query for with the last process instance query?

Are you looking for the process instance that created the given case instance via a call activity?
Or are you looking for the business key of the case instance?

Cheers,
Thorben

Hi Thorben,

I only want to receive the businessKey in both cases.
It works in the case of the Task.
It does not works in the case of CaseExecution.

But maybe there is an other possibility to receive the businessKey.

Hi @BerndVarga,

I think you are mixing up process instances and case instances. For a case instance, you can get the business key like so:

CaseExecution caseExecution = ...;

CaseInstance caseInstance = caseService.createCaseInstanceQuery().caseInstanceId(caseExecution.getCaseInstanceId()).singleResult();

String businessKey = caseInstance.getBusinessKey();

Cheers,
Thorben

1 Like

Hi Thorben,

thanks for your replies. Now the evaluation of businessKey works for a Task object.

I was a little bit confused about the difference between Task, Execution and CaseExecution.
Now I get from the TaskQuery to the available Task object. With the available instanceId it is possible to decide between Case and Task and to receive the current businessKey.

    private String getBusinessKey(String taskId) {
        String businessKey = null;
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        String id = null;
        
        if ( task.getProcessInstanceId() != null ) {
            id = task.getProcessInstanceId();
            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(id).singleResult();
            
            businessKey = processInstance.getBusinessKey();
        } else if ( task.getCaseInstanceId() != null ) {
            id = task.getCaseInstanceId();
            
            CaseInstance caseInstance = 
                processEngine.getCaseService().createCaseInstanceQuery().caseInstanceId(id).singleResult();
            businessKey = caseInstance.getBusinessKey();
        }

        return businessKey;
    }

It would be a improvement, if the task object will contain the businessKey as a attribute.

1 Like