Find all process instances by global variables

I want to create a service task that retrieves a list of process instances containing task variables from the map I’ve provided.

I wrote it like this , but its sometimes works sometimes not, what I am doing wrong ?

public List<ProcessInstance> findProcessInstanceByPayload(Map<String, Object> payload) {
    ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery();
    for (String str : payload.keySet()) {
        processInstanceQuery.variableValueEquals(str, payload.get(str));
    }
    log.debug("process instance query [{}]", processInstanceQuery);
    return processInstanceQuery.list();
}

Hello my friend!

I don’t know the entire context of your application… since you’re using Java, I imagine it’s Spring… try adding @Transactional to your service, and check if the error persists, please.

William Robert Alves