AnsweredAssumed Answered

Find process instance and modify variable

Question asked by active-e.t. on Oct 10, 2016
Hi
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)) {
                  piList.add(processInstance);
               }
            }
         }
         if(piList.size()==1) {
            ProcessInstance processInstance = piList.get(0);
            LOGGER.info("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());
            LOGGER.info("process date_mes value value is now " + processInstance.getProcessVariables().get(ConstantsBpmn.VAR_DATE_MES));
         }
         else if (piList.isEmpty()){
            LOGGER.info("There is no process to update date_mes");
         }
         else {
            LOGGER.info("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 !

Outcomes