Seve Filter Tasks in Java

Hi at all,
In the Camunda tasklist is the possibility to filter current tasks for specific variables:
image

On the right side is the save symbol, if you click it you will get saved options to filter existing tasks:
image

In the tasklist it is no problem to store those filter options and reuse them. However I want to create some of those filter tasks in my java code, so that the users dont have to create them theirselves.
Is there a way to do it?

Thank you :slight_smile:

Hi @MarvinKern,

Have a look at below topic in camunda blog

1 Like

Ah so you create a query:
TaskQuery myTasksQuery = taskService.createTaskQuery().taskAssignee("daniel.meyer").active();

and then you put the query in the filter like this:
myTasksFilter.setQuery(myTasksQuery);

I will try this, thanks a lot :slight_smile:

1 Like

Ah no does not work this way…
With this you just set, which tasks are shown by the filter, not how they are sorted…

Thanks for you answer, but the link does not show how to sort the tasks…

Hi @MarvinKern,

Try to use orderByProcessVariable, orderByExecutionVariable or orderByTaskVariable as in below example. You can either do asc() or desc() order.

TaskQuery myTasksQuery = taskService.createTaskQuery().taskAssignee("daniel.meyer").active().orderByProcessVariable("<VAR_NAME>", ValueType.STRING).asc();

Ahh Thank you for the information.

So I tried it, but I am using an or() statement and I am getting following error message:
Caused by: org.camunda.bpm.engine.ProcessEngineException: Invalid query usage: cannot set orderByProcessVariable() within 'or' query

Is there any workaround?

Thanks in advance :slight_smile:

Hi @MarvinKern,

Could you please post your task query statement.
Please make sure that orderByProcessVariable comes after endOr()

TaskQuery myTasksQuery = taskService.createTaskQuery()
        .or()
        .active()
        .taskUnassigned()
        .endOr()
        .orderByProcessVariable("<VAR_NAME>", ValueType.STRING)
        .asc();
1 Like

Ahh okay, thank you for the hint :slight_smile:
What is the asc() method doing?

Hi @MarvinKern,

You can either use asc() to sort in ascending or desc() to sort in descending.

Thank you for your help :slight_smile:
I tried your solution, but unfortunately the tasks I created are not filtered by the variable I chose :frowning:
I still have to create a sorting manually …

Hi @MarvinKern,

What do you mean by “not filtered”?
Filtering is different than sorting.

Could you please share your task query.