Build a Dynamic Task Query

I am looking for a method to create a complex Task query using a combination of and/or statements. I realize the REST API cannot support this, so I am looking to do this in a groovy script. The criteria will involve different processvariables at runtime, with a varying number of criteria.

List<Task> taskList = taskService.createTaskQuery().or()
		.processVariableValueEquals("someId1", searchId1)
                .processVariableValueEquals("someId2", searchId2)
                .endOr()
		.list();

Is there a way to build the criteria elements at runtime through reflection or something like that?

Could you please describe more specifically what you mean? All the methods you called in a chain are just normal java methods which of cause can be called via java reflection. Hence I don’t quite understand what’s your problem.

@Bill_Powell you can build the JSON payload dynamically at runtime , and then pass the JSON payload in request body.

Thank you, fml2. I am probably revealing my inexperience with java reflection. I am researching a solution and will post the answer here. I think I have the answer for dynamically creating the chain via invoke.

Thanks aravndhrs. I am looking for a solution using the java methods since the REST API does not support the combination of And an Or conditions. I am dealing with large result-sets and manipulating the arrays in code would be slow.

It turned out that no reflection was necessary and groovy allows chaining methods within a loop. In this case Tasks are selected for a User based on processvariables. Users are ‘allowed’ to work on Tasks for which the processvariables match one of the users approved list.

def engine = execution.getProcessEngineServices()
def taskService = engine.getTaskService()
def query = taskService.createTaskQuery()
query = query.taskDefinitionKeyIn((String[])filters.get("Tasks"))
            //.processVariableValueLessThan("order_lock_until", timeStamp())
            .taskUnassigned()
            .followUpBeforeOrNotExistent(new Date())

filterCategories.each {
    switch(it.toLowerCase()) {
        "branches":
            query = orQuery(query, "branch", filters.get(it), "toInteger")
            break
        "company":
            query = orQuery(query, "company", filters.get(it))
            break
        "county":
            query = orQuery(query, "county", filters.get(it), "toInteger")
            break
        "customers":
            query = orQuery(query, "customer", filters.get(it), "toInteger")
            break
        "services":
            query = orQuery(query, "product", filters.get(it), "toInteger")
            break
        "states":
            query = orQuery(query, "state", filters.get(it))
            break
        "status":
            query = orQuery(query, "status", filters.get(it))
            break
        default:
            log("Warning: Unknown filter category $it")
            break;
    }
    log("$it: " + filters.get(it).size())
}

query = query.matchVariableNamesIgnoreCase().matchVariableValuesIgnoreCase()

selectedTasks = query.list()//query.listPage(0,1)