'How to get previous task local variables of a running process instance in activiti

I am developing a spring boot application with activiti as the workflow engine. The activiti-spring-boot-starter dependency version is 7.1.0.M6 and spring-boot-starter-parent version is 2.6.7.

I have defined a BPMN 2.0 diagram using activiti-modelling-app and I am now starting the process instance. After completing a task, I want to access its task local variables when processing the next task. I am unable to figure out the api for it.

I tried using the historyService as below but with no luck. I get the result list as empty everytime with different apis (finished(), unfinished() etc)

HistoricTaskInstance acceptMobile = historyService.createHistoricTaskInstanceQuery()
                .processInstanceId(processInstanceId)
                .taskName("my-task1")
                .singleResult();

Can someone guide me on what could be the right api to use to get the local variables of a previously completed task?

Thanks.



Solution 1:[1]

The best way to transfer variables between tasks is to use execution variables with DelegateExecution

execution variables are specific pointers to where the process is active, for more information, see apiVariables

Let say you have Task-A and Task-B with different listeners

here's how to use execution variable from Task-A to Task-B:

@Component("TaskListenerA")
public class TaskListenerA implements TaskListener {

    @Override
    public void notify(DelegateTask task) {
         DelegateExecution execution = task.getExecution();
         if("complete".equals(task.getEventName()) {
             String myTaskVar = (String) task.getVariable("taskAvariable")
             execution.setVariable("exeVariable", myTaskVar);
         }
    }
}
@Component("TaskListenerB")
public class TaskListenerB implements TaskListener {

    @Override
    public void notify(DelegateTask task) {
         DelegateExecution execution = task.getExecution();
         String myVariable = execution.get("exeVariable");
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1