Filter task list using variables by REST API


#1

I have a multi instance sub-process with different tasks. When I ask for task instance variables I get list of expected variables like this:

{
“nrOfActiveInstances”: {
“type”: “Integer”,
“value”: 3,
“valueInfo”: {}
},
“myValue”: {
“type”: “Long”,
“value”: 9,
“valueInfo”: {}
}
}

myValue variable is set for each task instance independently and it can happened that two tasks have same value. Now, I want get list of tasks with given myValue value. I use /task (post) rest API call like this:

{
“taskVariables”: [{“name”: “myValue”,
“value”: “9”,
“operator”: “eq”
}],
“processDefinitionName”: “Simple process”,
“taskDefinitionKey”: “Test task”
}

without success - list is always empty. If I remove taskVariables from request I receive all my tasks irrespective of myValue current value. How can I filter tasks with specified variable value ?


#2

Hi @Kikol,

are you sure that the variable “myValue” is stored in the scope of the task?

It can be that the variable is stored in a parent scope (i.e. on the execution). When you query task variables then the result also contains the variables of the parent scopes.

Try to filter the tasks by processVariables.

Best regards,
Philipp


#3

Hi @Philipp_Ossler,
Yes, I’m sure they are in a valid scope. Unfortunately /task/{id}/variables/ do not return variable scope but i see it in Cockpit (I have multi instances of such variable due to multi instance sub-process). I already try with processVariables and even caseInstanceVariables without success. I also observe some phenomenon when using processVariables filter: when I change my operator from eq to neq I got full list of my tasks. Same has no effect for taskVariables - for me it looks like taskVariables list is always null and any operator applied to it returns empty set.


#4

Hi @Kikol,

can you provide a failing test case?
This would make it easy to reproduce the issue.


#5

Yes, no problem. I just create simple process: TestVariables.bpmn (7.0 KB)
Second task always set variable named myValue as a current loopCounter.
Here is my test case:
I start process and complete 2 or more tasks named “First task”. After that get list of tasks using /task REST API with body:

{“taskVariables”: [{“name”: “myValue”,
“value”: “3”,
“operator”: “eq”
}],
“processDefinitionName”: “Test variables”,
“taskDefinitionKey”: “task2”
}


#6

In your BPMN process, the variable myValue is stored as local variable of the execution. It is not stored in the scope of the task because it is declared as input parameter of the user task.

Currently, it seems that it’s not possible to filter tasks based on local process variables. It only works with (global) process variables.

You can use TaskListener to copy the variable into the scope of the task or change the query.

Best regards,
Philipp


#7

Hi @Philipp_Ossler
I can’t use global process variable here cause each task instance will override previous value. Could you elaborate a little bit more about TaskListener or provide an example how to copy variable into task scope?


#8

Hi @Kikol,

the task listener could look like this:

${task.setVariableLocal("myValue", myValue)}

(as expression on create task)

Does this help you?


#9

Hi @Philipp_Ossler
Not really. I setup task listener as you suggest. Now I see both variables(I change name to myValueLocal in order not to be confused by duplicated variables names).


Strange that both variabls are visible in a SecondTask scope. Nevertheless, there is no response for request:

{“taskVariables”: [{“name”: “myValueLocal”,
“value”: “2”,
“operator”: “eq”
}],
“processDefinitionName”: “Test variables”,
“taskDefinitionKey”: “task2”
}
However when I use neq operator all tasks are returned including those with myValueLocal set to 2 - that the difference when compared to version without task listener.
Basically, I need to check whether a multi instance task for specific conditions already exists in order not to create duplicated task. Maybe my approach of using local variables is not correct.


#10

I got it :slight_smile:
Task variables filters are evaluated using Strings whereas my variable is of type Integer. I was confused by documentation which says:

I was convinced that Integer is simple cast to Number which is not true. When I deeply look at the DB query i saw something like this

and EXISTS (select ID_
from ACT_RU_VARIABLE
WHERE NAME_= #{frch_queryVariableValue_2.name}
and RES.ID
= TASK_ID

and (( TYPE_ is not null and TYPE_ = #{frch_valueCondition_3.type}
and TEXT
is not null and TEXT
= #{__frch_valueCondition_3.textValue} )
))

for me it sounds like you never take into consideration numbers nor booleans. Anyway I solve my issue by explicit casing my local variable to String like this:

${task.setVariableLocal(“myValuetxt”, myValue.toString())}

@Philipp_Ossler thank you for your help