2 Replies Latest reply on Mar 26, 2015 5:19 AM by jimmy001

    Access process instance variable from TaskEventListener

    jimmy001

      Hello,

       

      does anybody know a way to access a not previously persisted variable value from a TaskEventListener?

      When starting a process I add some process variables to the instance. Now I would like to execute an action, when

      the first task in the process has been added using one of the values. Something like the following does not work:

       

       

      @Override
          public void afterTaskAddedEvent(TaskEvent event) {
              long processInstanceId = event.getTask().getTaskData().getProcessInstanceId();
              LOG.trace("afterTaskAdded " + event.getTask().getTaskData().getProcessInstanceId());        
              String processInstanceVar = (String) processService.getProcessInstanceVariable(processInstanceId, "$ae_process_label");
              // DO something with processInstanceVar        
          }
      

      The process instance can't be found. I assume, since it has not been commited to the database yet when executing the method.

       

      Thx for your help.

        • 1. Re: Access process instance variable from TaskEventListener
          swiderski.maciej

          you should not attempt to access process variables from within task listener. You should only rely on what variables given task instance has to avoid any tight coupling and inconsistent state. So make sure you pass process variables to the task and then you will not need to look up process instance.

           

          HTH

          • 2. Re: Access process instance variable from TaskEventListener
            jimmy001

            Well, ok.

            I would like to add one additional value too ALL tasks, when they are created (the concrete value is different from task to task).I replaced org.jbpm.services.task.impl.model.TaskImpl by my own class in the persistence.xml

            What would you suggest to accomplish this?

             

            A second point: How is it possible to get access to the task variables in the taskevent listner?

             

            List<Comment> comments = userTaskService.getCommentsByTaskId(event.getTask().getId());

             

            results in :

             

            [org.jbpm.services.task.persistence.TaskTransactionInterceptor] (default task-25) Could not commit session: org.jbpm.services.api.TaskNotFoundException: Task with id 84 was not found.

             

            Thx.