Task Query with Element Variable in Flowable

109 views Asked by At

I have a workflow with a sub-process running in parallel.

Bpmn workflow with sub process

The relevant xml look like that:

<process id="hello" name="Hello" isExecutable="true" flowable:candidateStarterGroups="flowableUser">
    <subProcess id="subProcess" name="Parallel Sub Process">
      <multiInstanceLoopCharacteristics isSequential="false" flowable:collection="insurers" flowable:elementVariable="insurerId">
      </multiInstanceLoopCharacteristics>

The workflow is started with a process variable ‘insurers’ containing a set of ifs.

String start(Set<String> insurers) {
    var process = runtimeService.startProcessInstanceByKey("hello", Map.of(INSURERS, insurers));
    return process.getProcessInstanceId();
}

The sub-process is configured as follow:

sub process configuration

I.e., every element of the collection instantiates a sub-process.

How can I query the user task by searching the element variable.

I tried the following:

  1. Querying the process variable
void completeTask(String processInstanceId, String insurerId){
    List<Task> tasks = taskService.createTaskQuery()
        .processInstanceId(processInstanceId)
        .processVariableValueEquals(INSURER_ID, insurerId)
        .list();
  1. Querying with task variable
void completeTask(String processInstanceId, String insurerId){
    List<Task> tasks = taskService.createTaskQuery()
        .processInstanceId(processInstanceId)
        .taskVariableValueEquals(INSURER_ID, insurerId)
        .list();

This doesn’t return any task.

  1. Querying by hand That last one works, but is not really how I would like to solve the problem
void completeTask(String processInstanceId, String insurerId){
    List<Task> tasks = taskService.createTaskQuery()
        .processInstanceId(processInstanceId)
        .processVariableValueEquals(INSURER_ID, insurerId)
        .list()
        .stream().filter(task -> {
            Object variable = taskService.getVariable(task.getId(), INSURER_ID);
            return Objects.equals(variable, insurerId);
         })
         .toList() ;

So how can I query an element variable?

1

There are 1 answers

0
Valentin Zickner On

The tasks are in the same process instance, this is why the first query returns both. The variable is however not task scoped, it's execution scoped and there is no filter for execution scope on the level of a task query, that's why the second approach doesn't work.

It is recommended to search either by a business attribute like assignee or category.