1
votes

While reading (https://docs.camunda.org/manual/7.5/user-guide/process-engine/variables/) I am not sure how you retrieve a variable?

At the moment I am struggling to find out how to access previously set process variables. What I tried is:

I have a simple bpmn process in which i have start event, 1 service task and end event, I am starting my process by passing 2 variables (a&b) and my service task is implementing following java class:

public class Addition implements JavaDelegate {

    public void execute(DelegateExecution exe) throws Exception {

        System.out.println("Inside calculator again");

        Integer x = (Integer) exe.getVariable("a");
        Integer y = (Integer) exe.getVariable("b");
        int add = x+y;
        System.out.println("Addition of two number is"+add);
        exe.setVariable("add",add);
}

I am starting my process as follows:

public void sayHello(ProcessEngine processEngine)
{
    Map<String,Object> variables = new HashMap<String, Object>();
    variables.put("a", 3);
    variables.put("b", 5);
    ProcessInstance instance=  processEngine.getRuntimeService().startProcessInstanceByKey("Process_3", variables);
}

I want to access add variable (present in Addition class) in sayHello class? As process has been completed so I can't use runtimeService so I tried to use history service but couldn't find out any solution.

Is there any Java API which I can use or is there any other way?

1

1 Answers

3
votes

If you want to get all historic variable instances use the list method in the HistoricVariableInstanceQuery.

For Example

List<HistoricVariableInstance> variables = processEngine.getHistoryService().createHistoricVariableInstanceQuery.list();

If you want to get specific variables with the given name you can use the method variableName(String)

For Example:

List<HistoricVariableInstance> variables = processEngine.getHistoryService().createHistoricVariableInstanceQuery().variableName("myVar").list();

To get the variables of a specific process instance use the method processInstanceId

For Example:

 List<HistoricVariableInstance> variables = processEngine.getHistoryService().createHistoricVariableInstanceQuery().processInstanceId(processInstance.getId()).variableName("myVar").list();

See for further information the documentation of the HistoryService and HistoricVariableInstanceQuery