[Java Api] Conditionally built task query returns empty result set

When querying for runtime tasks using Java api with fluent builder like below works fine and returns the desired result.

Working task query:

List<Task> taskList = taskService.createTaskQuery()
         .processDefinitionKey("orderProcess").taskAssignee("tom")
              .taskAssigned().active().listPage(0, 30);

But if the query is built based on certain criteria it always returns empty tasklist for same input. Here I will have the search criteria as Object like below:

{
	"processDefinitionKey": "orderProcess",
	"taskAssignee": "tom",
    "taskAssigned": true,
	"active": true
}

Above payload will be sent from UI as TaskRequest.java. The class look likes below:

public class TaskRequest{
  private String processDefinitionKey;
  private String taskAssignee;
  private boolean taskAssigned;
  private boolean active;
}

Now my fluent builder task api construct will look like below:

Not working Task Query:

@Autowired
private TaskService taskService;

// below func always returns empty result set. why?
public List<Task> getTaskList(TaskRequest taskrequest){
    TaskQuery taskQuery = taskService.createTaskQuery();

    if (StringUtils.hasText(taskRequest.getProcessDefinitionKey())) {
        taskQuery.processDefinitionKey(taskRequest.getProcessDefinitionKey());
    }

    if (StringUtils.hasText(taskRequest.getTaskAssignee())) {
        taskQuery.taskAssignee(taskRequest.getTaskAssignee());
    }

    if (taskRequest.isTaskAssigned()) {
        taskQuery.taskAssigned();
    }

    if (taskRequest.isActive()) {
        taskQuery.active();
    }

    List<Task> taskList = taskQuery.listPage(firstResult, maxResults);

  return taskList;
}

Any suggestions why the above mentioned query is not returning desired results?

Hi @aravindhrs,

I guess the resulted TaskQuery instance of each call should be assigned to the taskQuery variable

if (StringUtils.hasText(taskRequest.getProcessDefinitionKey())) {
         taskQuery = taskQuery.processDefinitionKey(taskRequest.getProcessDefinitionKey());
    }
1 Like

Thanks @hassang . It works. Also i have written wrong query for task priority.

if (taskRequest.getTaskPriority() >= 0) {
	taskQuery = taskQuery.taskPriority(taskRequest.getTaskPriority());
}
if (taskRequest.getTaskMinPriority() >= 0) {
	taskQuery = taskQuery.taskMinPriority(taskRequest.getTaskMinPriority());
}
if (taskRequest.getTaskMaxPriority() >= 0) {
	taskQuery = taskQuery.taskMaxPriority(taskRequest.getTaskMaxPriority());
}

so it starts filtering with default value 0 for priority based fields. While debugging found this issue and changed the criteria from >= to > working fine.