taskService doesnt return active tasks

Hi All,

I am starting a process instance from springboot and once a task is created, I need to know the list of active user tasks with the current instance. so I have written below code in the create event of the task listener → notify(). but I am always getting null value. can you please help me identify the issue or provide a solution for my requirement.

List taskList = taskService
.createTaskQuery()
.processInstanceId(delegateTask.getProcessInstanceId())
.list();

Hi @syedkp ,

first I will try to explain the background, so here is what happens:

  1. Process starts
  2. Process reaches start of UserTask
    → process will be persisted to the database
  3. Task Listener triggers (I assume you assigned it to the “create” event)
    → The task is not yet persisted, so you are not able to query it

Depending on your use case we might find another approach which works for you. Would be great if you could provide more information, e. g.:

  • Why do you need to query for active tasks, when you have the DelegateTask provided?
  • Are there possibly multiple user tasks by using parallel gateways?
  • Are subprocesses in volved?

Kind regards
Adagatiya

Thanks @Adagatiya for your reply. Okay let me take a step back. So I have written code to trigger a camunda process as below.
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(“scoping_letter”,“127”,variables);

As you mentioned above, the process is initiated and the user task is created. I have create event which is invoked as the flow reached this task, I added another event (assignment) to capture the task details through taskService(). As shown below.

if(delegateTask.getEventName() == “create”) {
taskService.claim(delegateTask.getId(), “demo”);
}

if(delegateTask.getEventName() == “assignment”) {
List taskList = taskService
.createTaskQuery()
.processInstanceBusinessKey(“127”)
//.processInstanceId(delegateTask.getProcessInstanceId())
.list();
taskList.stream().forEach(task → System.out.println(“Task Id :::”+task.getId()+",Process id :::"+task.getProcessInstanceId()));
System.out.println("Task List :::: " + taskList);

Still I dont see the task list getting returned. Please see the bpm file attached which looks simple from outside.
scoping-letter.bpmn (9.2 KB)

Hello @syedkp ,

the reason why your assignment listener will also not find user task instances in the database is the same why the create listener will not:

The user task is still not persisted as assignment happens on creation. This is because you have done user assignment in the BPMN model.

Is there a specific reason why you would like to have a list of active user tasks in the listener?

Also, here is a corrected version of your model. I found a problem with unconnected elements and I also aligned them a bit:

scoping-letter.bpmn (9.0 KB)

Jonathan

@jonathan.lukas . Thanks for your reply. so basically, we are using camunda as headless implementation. so UI is in angular. user clicks on create workflow button and inturn calls the springboot api. Here we are triggering a camunda instance. now we have to capture the task details within camunda and perform certain manipulations and then insert these details into application db. so basically every action that happens on UI, will be done through java code on camunda process/task and saved in application db. Hope I havent confused you. please let me know if there is any other way to capture these details.
Thanks for the bpmn correction.

Hello @syedkp ,

this sounds reasonable. Why wouldn’t you place the task query outside of the engine api call? There, you have no running transaction and can be sure everything is saved to the database.

Or, you could use the listeners to just sync the state of the current task to your application db.

I hope this helps

Jonathan

1 Like

@jonathan.lukas - Thanks for your suggestions.

I understood the 2nd option. So basically you mean, when the event is create → Insert record in db and upon assignment → update the status and complete → close the task.

But 1st option I am not sure, when you say outside engine api call. can you give an example or brief more on this.

You mention a springboot api here. Is this your api or the camunda rest api?

@jonathan.lukas - This is springboot application api.

Hello @syedkp ,

so then, you will receive a request and call the Camunda Java API?


@PostMapping
public StartProcessResponse startProcess(@RequestBody StartProcessRequest request) {
  ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(...);
  List<Task> tasklist = processEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).list();
  StartProcessResponse response = new StartProcessResponse();
  response.setProcessInstanceId(processInstance.getId());
  response.setTaskIds(tasklist.stream().map(Task::getId()).collect(Collectors.toList()));
  return response:
}

Something like this?

Jonathan

@jonathan.lukas - ok sure. i will try this solution as well. Thanks