Variables set via DelegateExecution.setVariables in not accessible through HistoryService

My BPMN structure has 2 pieces divided by a user-task. As the user-task activity is triggered (wait-state), I want to access the HistoryService and get all the variables and log in my use-case database.

I’m doing this in the last ServiceTask (before the user-task activity),

public void execute(DelegateExecution execution) {
        Map<String, Object> outputs = new HashMap<>();
        //logic to generate outputs
        execution.setVariables(outputs);
}

However, once the above ServiceTask is completed, and My UserTask activity get’s triggered (moves to wait state) and during this flow, I’m trying to get the variables from the HistoryService and the outputs set above is not present in the HistoryService list.

My HistoryService access looks like,

List<HistoricVariableInstance> flowableVariables = getProcessEngine().getHistoryService()
                .createHistoricVariableInstanceQuery().processInstanceId(procInstanceId).list();

Also in the flowable xml,
The service task has async = true and triggerable = false

Please help here.

Hi,

the possible problem is that transaction is not committed to the DB yet, that’s why history service vars are not present yet.
To propose a solution I would need to understand the usecase. The variables are set correctly for the execution instance.
Regards
Martin

Hi Martin,

This is the use-case,

  1. We have a ServiceTask which calls an external service. In this Task async = true and triggerable = true. Once we receive callback from external service, we call triggerAsync() method and complete this.

  2. Post this, we have another ServiceTask which has async = true and triggerable = false which does completes it’s work within the execute() implementation of JavaDelegate and it writes some outputs to the Flowable engine (as mentioned in the description).

  3. Post Service task mentioned in point 2, a userTask activity is trigger (we are notified as we binded an ActivityStartListener to this). In the flow of ActivityStartListener, I’m accessing HistoryService and trying to access the outputs generated in ServiceTask (mentioned in step 2) and notify the user’s about the Output via email. Hence, at this place, when accessing HistoryService, I’m not able to access the vars written in step 2.

Hi,

If you want to make a variable value accessible by the user task listener on create event you have to make a user task async.
It is still a hack and it makes solution more complicated - in fact I do not like the solution, but it will allow you to get values by hist service query.

Regards
Martin

do you mean to say?,

<userTask flowable:async="true" id="task1" />

Ignore. my bad. I made a mistake when setting execution.setVariables(outputs) and that’s why it wasn’t visible. HashMap didn’t have keys when setting up variables which was expected. Corrected the execute() method logic and it’s visible now.

Thanks