TaskQuery for tasks of several processes

Is using or() in a task query legal for process instance identifiers?

I am using it like this:

        TaskQuery taskQuery = _taskService.createTaskQuery().or();
        for (String subProcessId : processIds) {
            taskQuery = taskQuery.processInstanceId(subProcessId);
        }
        tasks = taskQuery.endOr().initializeFormKeys().list();

but it seems to use only one processInstanceId. Probably I am misusing the syntax…

Yes, your syntax is not complete / not correct.
See the help at
https://docs.camunda.org/manual/7.8/user-guide/process-engine/process-engine-api/#or-queries
for an example including detailed explanation.

Thanks a lot for the rtfm link, I’ve read it.

Sorry, English is not my native language, some nuance is missing me, I can’t figure out where my code is wrong.

A correct code sample with multiple processInstanceIds in a task query would go, thank you!

This should work:

        TaskQuery taskQuery = this.taskService.createTaskQuery();
        for (String subProcessId : processIds) {
            taskQuery = taskQuery.or().processInstanceId(subProcessId).endOr();            
        }
        List<Task> tasks = taskQuery.initializeFormKeys().list();

You need to or every single processInstanceId. Otherwise only the last value applied will be used, all valued applied before are overwritten.

Sorry, it made things only worse, no task is selected at all.

What you’re suggesting is effectively equals the following:

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

I searched the doc page you’ve sent me to for the “endOr” substring, and in no case there is such a construct. Sorry if I am too dumb or it’s an extended April 1st, I will just copy all examples from the docs for everybody to see how puzzled I am:

List<Task> tasks = taskService.createTaskQuery()
  .taskAssignee("John Munda")
  .or()
    .taskName("Approve Invoice")
    .taskPriority(5)
  .endOr()
  .list();

List<Task> tasks = taskService.createTaskQuery()
  .or()
    .processVariableValueEquals("orderId", "0815")
    .processVariableValueEquals("orderId", "4711")
    .processVariableValueEquals("orderId", "4712")
  .endOr()
  .list();

This one is proclaimed to be wrong as only the last filter is taken into account, because it is “a non-variable-filter-criterion” - whatever this term may mean.

List<Task> tasks = taskService.createTaskQuery()
  .or()
    .taskCandidateGroup("sales")
    .taskCandidateGroup("controlling")
  .endOr()
  .list();

I suspect that my processInstanceId may fall into the category of “non-variable-filter-criteria” which is forbidden to be used in Or constructs

That’s why I decided to turn to the community hoping I am not under some sort of personal sanctions

remove the or() / endOr() surrounding the first processInstanceId condition:

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

From my understanding of the docs, thsi should lead to something like
process_instance_id_ = ? OR process_instance_id_ = ? OR process_instance_id_ = ?

Sorry for reviving such an old post but I’ve run into this and the responses didn’t work for me. This code sample works for me.

TaskQuery taskQuery = _taskService.createTaskQuery();
taskQuery = taskQuery.or();
for (String subProcessId : processIds) {
taskQuery = taskQuery.processInstanceId(subProcessId);
}
taskQuery = taskQuery.endOr();

Hope that helps anyone else who is having this problem