Task Query with Element Variable

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

simple-workflow

or here an xml extract:

  <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:

paralell-config

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();

This does not work, both (assuming I started the workflow with 2 insurerIds) tasks are returned.
2. 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.
3. 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

Why do you want to query by variable? If you e.g. set the assignee of the user task to ${insurerId}, you can add .taskAssignee() to your query and it will be returned.

The reason why 1) returns 2, because there are indeed 2 tasks where the filter ‘process instance with this variable’ is true.
2) doesn’t work, because the variable is process scoped, not task scoped.