TaskQuery with multiple processInstanceId

Hi there,

I’m looking for a way to find currentTask for a list of processInstanceId.

https://forum.camunda.io/t/taskquery-for-tasks-of-several-processes/6759

I watched this post and solutions who were submitted but nothing works.

tasks = _taskService.createTaskQuery()
                .or().processInstanceId(processIds[0]).endOr()
                .or().processInstanceId(processIds[1]).endOr()
                .or().processInstanceId(processIds[2]).endOr()
                .initializeFormKeys()
                .list();

This solution doesn’t work because between each “.or()”, engine will use “AND”. And use only one “or()” doesn’t work neither because each processInstanceId will be replaced by the next one.

Right now, I’m using NativeQuery but I’m looking for a standard way to do it.

    StringBuilder sb = new StringBuilder();
    sb.append("SELECT * FROM ACT_RU_TASK ");
    sb.append(" WHERE ");
    sb.append(StreamUtil.streamopt(processInstanceIds).map(a -> "(PROC_INST_ID_ ='" + a + "')").collect(Collectors.joining(" OR ")));

    return taskService.createNativeTaskQuery().sql(sb.toString()).list();

In my opinion, best solution would be to add “processInstanceIdIn(…)” in taskQuery. Is there another way than NativeQuery right now ? (I’m using Camunda 7.12)

Thanks a lot,

Hi Matthieu,

Welcome to the Camunda community.

In the upcoming alpha release (7.13.0-alpha1, release date: this Friday),
you can find another filter option to the task query that will helpful for your use case - processInstanceIdIn.
You will be able to fetch the tasks of different process instances via:

tasks = taskService.createTaskQuery()
                .processInstanceIdIn(processIds[0], processIds[1], processIds[2])
                .list();

You can check it in Rest API docs as well:
https://docs.camunda.org/manual/develop/reference/rest/task/get-query/#query-parameters
The 7.13.0 minor release is scheduled for the end of May.

Best regards,
Yana

2 Likes

Hi Yana,

Thanks a lot, that’s a very good news !

I will upgrade to 7.13 at the end of May, then, and remove my NativeTaskQuery.

Best regards,
Matthieu

1 Like