How can I access a case instance from a process task?

Hi,

I would like to access a case instance in a process task script (groovy). I tried to put “caseExecution” in the script but not works. I could only access the case id but I don´t know how to access a case instance from a case id in a script.

With this case instance, I want to set a case variable and start manually a case task.

Any hint to do it ?

Tks,
Carlo Borsoi

Hi Carlo Borsoi,

You can make a case instance query via CaseService#createCaseInstanceQuery and then filter via CaseInstanceQuery#subProcessInstanceId where the argument is the ID of the process instance. That should return the parent case instance.

Cheers,
Thorben

Hi @thorben ,

Thanks for reply.

I was solved it using execution, in script, I get the case based on it, because the case instanted the process. Let me share this code:

process_engine = execution.getProcessEngineServices()
case_service = process_engine.getCaseService()
variable1 = case_service.getVariable(caseID, "variable1")

But, to start manually a case task, I used a case instance query, as you sugest;

caseTask = case_service.createCaseExecutionQuery().caseInstanceId(caseID).activityId("PlanItem_TASK1").singleResult();
if (caseTask.isEnabled()) {
	case_service.withCaseExecution(tarefaExec.getId()).manualStart()
}

I am new in Camunda and any comments on this code are welcome !

Cheers,
Carlo

Hi Carlo,

The code looks good to me.

Cheers,
Thorben