AnsweredAssumed Answered

Find process instance and modify variable

Question asked by active-e.t. on Oct 10, 2016
I have a BPMN process with two parallel gateways like this :

parallel gateway 1 >
- service task
- timer
< parallel gateway 2

I want, with the the service task code below, to update the "date_mes" variable used by the timer with the date of today.
This would unlock the timer and let the flow continue.

         List<ProcessInstance> piList = new ArrayList<ProcessInstance>();   
         for (ProcessInstance processInstance : allPiList) {
            if (!processInstance.getProcessVariables().isEmpty()) {
               if (processInstance.getProcessVariables().containsKey(ConstantsBpmn.VAR_DATE_MES) && processInstance.getProcessVariables().containsValue(poId)) {
         if(piList.size()==1) {
            ProcessInstance processInstance = piList.get(0);
  "Found the process with date_mes to update :" + processInstance + " updating date_mes with " + (new DateTime()).toString());
            runtimeService.setVariable(processInstance.getId(), ConstantsBpmn.VAR_DATE_MES, (new DateTime()).toString());
  "process date_mes value value is now " + processInstance.getProcessVariables().get(ConstantsBpmn.VAR_DATE_MES));
         else if (piList.isEmpty()){
  "There is no process to update date_mes");
         else {
  "There is more than one process to update date_mes");

The service task code is executed but doesn't find any process instance. "piList" is empty, unless I replay the process then it finds a processInstance with an activityId equal to my first gateway name.

How can I get the current processInstance and update the variable on the first time ?

Any help is welcome, thanks !