What this code does?

Hi, This is my first post and I’m learning Camunda.

I have one query here.

Task task = taskService.createTaskQuery().processInstanceId(processId).active().singleResult();

Could you please explain what this code does?

If I have two manual tasks in my process. Which manual task it would return ?

Hi @cofactor,

Queries for tasks.

in a certain process instance and only active (i.e. not suspended) tasks.

Assumes there is only one result. If there are more results, this method throws an exception.

None :). The term task here refers to a work item for a human, to instances of BPMN user tasks (e.g. accessible via Camunda task list). Unfortunately, this term is overloaded and has a different meaning in BPMN.

I hope this makes sense.

Best regards,
Thorben

Edit: By the way, if you need a representation of all currently active activities in a process instance, then have a look at the API RuntimeService#getActivityInstance.

I get one task. Its a user task.

Ah, I understood you meant BPMN manual tasks, i.e. these guys:

grafik

No.

This one
task1

I have two of such. … you call it user task…right ?

the code returns this task.

how the code is working ? Is it the code is giving immediate next user task ?

Yes, those are user tasks. Could you please post the complete BPMN model and describe which state the process instance is in when you make the query?

Its really a very big model. I am showing you only the relevant part

I’m at validate ISBN service task and I call this code.

Task task = taskService.createTaskQuery().processInstanceId(processId).active().singleResult();

I get that user task.

You were talking about two user tasks, though. Where is the second one?

There are 7 service tasks after this user task…and then that second user task comes.

Okay, if the process instance is at Get Selected ISBN, then the query returns only the work item that corresponds to this activity. It does not include tasks for activities that have not yet been reached.

can we call this as “active” ?

since
processInstanceId(processId).active() - in a certain process instance and only active (i.e. not suspended) tasks.

No, this is a different concept. There are APIs to suspend tasks (e.g. TaskService#suspendTask), which ensure it can not be worked on (completed) until it is activated again. This still requires that process execution has reached the user task in the BPMN model.

Okay.

Can we achieve the same if I just do this instead of the earlier code ?

Task nextTask = taskService.createTaskQuery().processInstanceId(processId).singleResult();

// I just removed active()

Does active() matter here ?

No, active() is not required here.

1 Like