Get active user tasks

Hi All,

I have set a create task listener using java delegate. when I try to get all the user tasks using taskService within the notify(), I am not able to get any task list. below is the java code, I have attached the bpmn file for reference.

@Component()
public class TaskEventNotify implements TaskListener {

@Autowired
TaskService taskService;

@Override
public void notify(DelegateTask delegateTask) {
       List<Task> taskList = taskService
            .createTaskQuery()
            .processInstanceBusinessKey("114")
            //.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);

scoping-letter.bpmn (9.1 KB)

}

Your code is running fine for me. Can you check whether there is a process instance with business key “114”?

I used the following model for testing:
test_example.bpmn (2.4 KB)

  1. I started two process instances:
    a) one with business key “114”
    b) one with business key “115”
  2. I had two tasks in my task list
  3. I picked a task and completed it
  4. The console showed me the task of the instance with business key “114”:
Task Id :::d218d235-278c-11ed-bede-84a9385a77fb,Process id :::d21772a2-278c-11ed-bede-84a9385a77fb
Task List :::: [Task[d218d235-278c-11ed-bede-84a9385a77fb]]

Thanks @StephanHaarmann. When I run the application and create event is triggered, I am not able to get the task details using TaskService though I am able to see the details using delegateExecution.getId(). If I try to fetch the tasks for a given process through a different method (using the above same code) I am able to get the details. So the issue is only within the create event listener that the TaskService doesnt return active tasks.

When the create event occurs and your task listener is called, the current task instance is not yet registered and cannot be queried via the TaskService. However, other tasks can. You can easily confirm this by starting a process instance with business key 114, advance it to the task with the tasklistener. The task won’t be in your output. If you start a second process instance and also advance it to said state, then the task listener will correctly find the task of your first process instance.

Thanks @StephanHaarmann . This is the issue I was facing when I said, I am not able to get the active tasks when a new task is created and create task listener is not returning me the list. So this is a known issue. But I was thinking that the task listener is triggered after the task is created, so the TaskService should be able to capture the recently created task right? unless there is some limitation.

The event listener should return all active tasks, except the one that has just been created. In my understanding, this is not an issue but a feature :wink:: The task has been created successfully, i.e., all properties have been set. The create event is raised and the corresponding TaskListener are called. The listeners can access and operate on the task before anything else happens. It, for example, has the power to override properties. After the execution of the TaskListener, the task is “published”/accessible via the task service - That’s at least how I see it.
You can use delegateTask.getEventName() to see if the Listener has been triggered by a create event and then create your own list of tasks including the currently active one.

1 Like

Thanks @StephanHaarmann :slight_smile: appreciate your response,

@StephanHaarmann - I think something is wrong here. When I create a process with a business key and use TaskService to get the active tasks for this process instance it doesnt return any record. when I create a new process and try to get tasks from previous process using business key it returns even though the task from previous instance was not accessed/opened. Now how do i get the active tasks as soon as it gets created. do you have any sample java code that you can share?