Task Query OR returning only one Task

    /**
    * Ids = List of Ids [1,2,3]
    */
    public void getTasks(String processDefinitionKey,
		final Map<String, Object> processVariables
            final List<Long> ids) throws UnsupportedDataTypeException {
	ProcessInstance processInstance = getProcessInstanceByBusinessKey(processDefinitionKey, processVariables);
	String processInstanceId =  processInstance.getProcessInstanceId();
	
	TaskQuery query = taskService.createTaskQuery().processInstanceId(processInstanceId).or();
	List<Task> initialList = taskService.createTaskQuery().processInstanceId(processInstanceId).list();  // Returns BOTH Tasks
	for(Long id :ids ) {
		query = query.taskName(id.toString());
	}
	List<Task> activeTaskList = query.endOr().list(); // Returns the LAST Task
	
	if (activeTaskList.size() > 0) {
    	taskService.complete(activeTaskList.get(0).getId(), processVariables);
	}
}

In my currently executing Process Instance - I have 4 tasks with the names 1, 2, 3, 4 running. But when run I this query - it always returns Task 3. What my intent is to get the Tasks with the names 1, 2, 3 and NOT 4. Seemed straightforward to use OR for this but it feels like I’m missing something. Any suggestions?

Hi @SemperFi1970,

Have a look at below post

TaskQuery for tasks of several processes

@hassang Thanks for the reply. I had tried that approach : essentially return the QUERY and assign the query to the same query object inside the for(Long id : ids) loop construct. I’ve updated the code to reflect that.

Didn’t seem to make any difference. The activeTaskList still returns the LAST task and not both despite the name matches.

Hi @SemperFi1970,

Okay, as per the docs, “A filter criterion related to variables can be applied multiple times within the same OR query” but " Whenever a non-variable-filter-criterion is used more than once inside a query, only the value which was applied last is utilized"

“To avoid this behavior, filter criteria with a trailing … In could be used” but no such filter criteria for task name

I think one valid solution would be to use a task variable which holds the task name value. then as per docs taskVariableValueEquals(variableName, variableValue) can be applied multiple times within the same OR query

1 Like

Nice!!
But I see the other In implementations (like activityInstanceIdIn or processDefinitionKeyIn) in the Camunda API but not for taskNameIn().
Is there a different way I can get to the In for taskName?

Hi @SemperFi1970,

Please see my suggestion above. I have edited my post.

Hey @hassang - I tried this approach as well. This still returns the last task. Anything I’m doing wrong here? I have set a local task variable with key as id on both tasks earlier. If i look up the tasks hard coded with that variable name as depicted below - it returns fine individually. But included into the or it still returns the last task:

		TaskQuery query = taskService.createTaskQuery().processInstanceId(processInstanceId).or();
	Task t1 = taskService.createTaskQuery().processInstanceId(processInstanceId).taskVariableValueEquals("id", "1").singleResult(); // Returns Successfully
	Task t2 = taskService.createTaskQuery().processInstanceId(processInstanceId).taskVariableValueEquals("id", "2").singleResult(); // Returns Successfully
	
	for(Long id :ids ) {
		query = query.taskVariableValueEquals("id", id.toString());
	}
	TaskQuery finalQuery = query.endOr();
	List<Task> activeTaskList = finalQuery.list(); // Returns the LAST Task

Hi @SemperFi1970,

That means filter criterion related to only process variables can be applied multiple times.

I am afraid that your only way is to try either native or custom queries

thanks @hassang. I will attempt so. Do you think having support for taskNameIn as an enhancement makes sense in this context?

Hi @SemperFi1970,

If your real case is the same case you described then you can just exclude Task 4 using the filter criteria taskNameNotEqual(name)

Unfortunately - The code above was a snippet of the whole. The tasks are dynamically generated since the Ids are passed in dynamically. Filter queries would need to be INCLUSIVE using the or paradigm as discussed above.