AnsweredAssumed Answered

How to retrieve the the return value of the last serviceTask

Question asked by petercahyadi on Feb 23, 2012
Latest reply on Jul 15, 2016 by dananemanoj
Hi, is there a way to retrieve the return value of the serviceTask at the end of the process ?

My BPMN file is as below
<definitions id="definitions" targetNamespace="http://activiti.org/bpmn20"
   xmlns:activiti="http://activiti.org/bpmn" xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL">
   <process id="testProcess" name="Test process">
      <startEvent id="theStart" />
      <sequenceFlow id='flow1' sourceRef='theStart' targetRef='javaService' />
      <serviceTask id="javaService" name="My Java Service Task"
         activiti:expression="#{myServiceTask.execute(name)}"
         activiti:resultVariableName="myResult" />
      <sequenceFlow id='flow2' sourceRef='javaService'
         targetRef='theEnd' />
      <endEvent id="theEnd" />
   </process>
</definitions>

And my Java serviceTask is as below


@Component
public class MyServiceTask {
   @Transactional
   public String execute(String name) throws Exception {
      
      List<MyTable> myTables = myTableMapper.selectByExample(null);
      int maxId = -1;
      for (MyTable myTable : myTables) {
         maxId = maxId < myTable.getId() ? myTable.getId() : maxId;
      }

      MyTable newMyTable = new MyTable();
      newMyTable.setId(maxId + 1);
      newMyTable.setName(name);
      myTableMapper.insert(newMyTable);

      return "Halo " + name + " !";
   }

   @Autowired
   private MyTableMapper myTableMapper;

}

and here is my main code, which is the one who started the process.

Map<String, Object> input = new HashMap<String, Object>();
input.put("name", "Peter");
ProcessInstance pi = runtimeService.startProcessInstanceByKey("testProcess", input);

i've been trying to retrieve the return value of the java service task, after the startProcessInstanceByKey() finishes executing. but no luck so far, any suggestion on how to do this ?

Outcomes