User Task get Task Listener detail

Hello,

I have task listener configured on an User task which will execute on complete event. However, I would like to get the details of this listener using the task object before even this event is triggered to perform some logic. I have the below code which gets me the task details from org.flowable.engine.TaskService. But I don’t see any info regarding the listeners on this object.

Task task = taskService.createTaskQuery().processInstanceId( processInstanceId )
.taskAssignee( taskAssignee )
.active()
.singleResult();

Is there any way i could get these details ?

Thanks

Hi @venkatesh

I think you have to go over the task model:

    Task task = taskService.createTaskQuery().processInstanceId(processInstanceId)
            .taskAssignee(taskAssignee)
            .active()
            .singleResult();
    
    if (task.getProcessDefinitionId() != null) {
        org.flowable.bpmn.model.Process process = ProcessDefinitionUtil.getProcess(task.getProcessDefinitionId());
        FlowElement flowElement = process.getFlowElement(task.getTaskDefinitionKey(), true);
        if (flowElement instanceof UserTask) {
            UserTask userTask = (UserTask) flowElement;
            List<FlowableListener> taskListeners = userTask.getTaskListeners(); 
        }
    }

Regards,
Simon

Thanks so much Simon. This makes sense. I was not aware of how to get the model object given a task. Will try this and update.

Hello Simon,

I had to execute the above logic as part of a new command. Other wise got an exception stating " org.flowable.common.engine.api.FlowableException: Cannot get process model: no current command context is active". Thank you for providing the solution !

Below is the final code.

 try
            {
                taskListeners = processEngine.getManagementService().executeCommand( new Command<List<FlowableListener>>()
                {

                    @Override
                    public List<FlowableListener> execute( CommandContext commandContext )
                    {
                        org.flowable.bpmn.model.Process process = ProcessDefinitionUtil.getProcess( task.getProcessDefinitionId() );
                        FlowElement flowElement = process.getFlowElement( task.getTaskDefinitionKey(), true );
                        if( flowElement instanceof UserTask )
                        {
                            UserTask userTask = (UserTask) flowElement;
                            List<FlowableListener> taskListeners = userTask.getTaskListeners();
                            return taskListeners;

                        }
                        return null;
                    }

                } );

            }
            catch( FlowableException fe )
            {
                fe.printStackTrace();
            }