Search code examples
javabpmncamunda

How to Retrieve execution from ProcessInstance


Just started with creating a flow in camunda and playing around with process variables. I can successfully create these process variables and then read them inside service tasks and then output them using Input/Output parameters. But I am struggling with again retrieving updated values of these parameters in the program which actually started this flow. Here are the snippets:

//Test case

RuntimeService rs = processEngine().getRuntimeService();
      Map<String, Object> variables = new HashMap<>();
      variables.put("percentageCompletion", "11");
      ProcessInstance processInstance = rs.startProcessInstanceByKey(PROCESS_DEFINITION_KEY, variables);

This percentageCompletion is accessible in service-task by doing

Object pc = execution.getVariable("percentageCompletion");// Update and set its new value

Now how to again fetch updated value of percentageCompletion in test case? I have already followed similar question Process Variables in Camunda-BPM


Solution

  • This is how I am able to fetch them in junit

     @Test
      @Deployment(resources = "process.bpmn")
      public void testParsingAndDeployment() {
     }
    
      @Test
      @Deployment(resources = "process.bpmn")
      public void testHappyPath() {
          RuntimeService rs = processEngine().getRuntimeService();
          Map<String, Object> variables = new HashMap<>();
          variables.put("percentageCompletion", "11");
          ProcessInstance processInstance = rs.startProcessInstanceByKey(PROCESS_DEFINITION_KEY, variables);
          System.out.println(processInstance.isEnded() + ", " + processInstance.isSuspended());
          VariableInstance vi = processEngine().getRuntimeService().createVariableInstanceQuery().processInstanceIdIn(processInstance.getId()).variableName("percentageCompletion").singleResult();
          System.out.println(processInstance.isEnded() + ", " + processInstance.isSuspended());
          System.out.println("********* " + processEngine().getHistoryService().createHistoricDetailQuery().processInstanceId(processInstance.getProcessInstanceId()).count());
    
          String executionId = processEngine().getHistoryService().createHistoricDetailQuery().processInstanceId(processInstance.getProcessInstanceId()).list().get(0).getExecutionId();
          System.out.println("executionId " + executionId);
    
          List<HistoricVariableInstance> hvis = processEngine().getHistoryService().createHistoricVariableInstanceQuery().executionIdIn(executionId).list();
          System.out.println("------------ " + hvis.size());
    
          for (HistoricVariableInstance hi : hvis) {
              System.out.println(hi.getName() + " : "+ hi.getValue());
          }
      }