Get candidate user, group and assignee from DelegateExecution

Hi,
I have implemented ExecutionListener with my class.
In the method,

public void notify(DelegateExecution execution) throws Exception

I’d like to retrieve from execution all information about current Human Task.
I’d like to know how I can get the assignee, user and candidate groups.

Any ideas?

Cheers,

Antonio

@antonio.luzzi,

I would recommend that you use a Task Listener instead of an Execution Listener for this purpose. In Camunda Modeler, you’ll see the ability to add a Task Listener immediately under the box that will let you add an Execution Listener. (Task Listeners can only be added for User Tasks.)

A Task Listener class should implement the TaskListener interface, which simply requires that the class implement the notify(DelegateTask task) method. The DelegateTask Object will then give you access to the following methods which will be of interest to you:

  • getAssignee() -> Retrieves the assignee for the User Task if one has been specified (which would be the case if the User Task has been claimed).
  • getCandidates() -> Retrieves a java.util.Set of IdentityLink Objects, which can hold either candidate groups or candidate users.

Here’s a quick and dirty implementation of the notify(DelegateTask task) method that can be set to run on “create” and “assign” and that will print the assignee or the candidates - if no assignee exists - to the console:

@Override
    public void notify(DelegateTask task) {

        if(task.getAssignee() != null) {
            String assignee = task.getAssignee();
            log.info("The assignee for task " + task.getId() + " is: " + assignee);
        }
        else {
            log.info("Task " + task.getId() + " is not yet assigned. Displaying all candidate users and groups...");

            Set<IdentityLink> identityLinks = task.getCandidates();
            if(identityLinks.isEmpty())
                log.info("No candidate users or groups exist for this User Task. Id: " + task.getId());
            else {
                for (IdentityLink identityLink : identityLinks) {
                    if(identityLink.getGroupId() != null)
                        log.info("Task " + task.getId() + " has an identity link for a group with id: "
                                + identityLink.getGroupId());
                    else if(identityLink.getUserId() != null)
                        log.info("Task " + task.getId() + " has an identity link for a user with id: "
                                + identityLink.getUserId());
                }
            }
        }
    }

Note that you can also use a script - e.g. using JavaScript - as a Task Listener as well. In that case, you’ll have access to the same task Object.

-Ryan

1 Like

Thanks to help me.

I have added a DelegateTask to every HumanTask.
In have extended AbstractProcessEnginePlugin and added NotifyHumanTask.

> @Component
> public class MyEventListenerPlugin extends AbstractProcessEnginePlugin {
> 
> 	
> 	@Override
> 	public void preInit(ProcessEngineConfigurationImpl processEngineConfiguration) {
> 		if (processEngineConfiguration.getCustomPostBPMNParseListeners() == null) {
> 			processEngineConfiguration.setCustomPostBPMNParseListeners(new ArrayList<BpmnParseListener>());
> 		}
> 		processEngineConfiguration.getCustomPostBPMNParseListeners().add(new NotifyHumanTask());
> 	}
> 
> }

Where NotifyHumanTask extends AbstractBpmnParseListener:

     public class NotifyHumanTask extends AbstractBpmnParseListener {       	
    	public void parseUserTask(Element userTaskElement, ScopeImpl scope, ActivityImpl activity) {

    		TaskDefinition taskDefinition = ((UserTaskActivityBehavior) activity.getActivityBehavior()).getTaskDefinition();
   		taskDefinition.addTaskListener(TaskListener.EVENTNAME_CREATE, new TaskListener() {

    			public void notify(DelegateTask delegateTask) {
    				// send notification message
    			}
    		});         
}       

}

Thansk Ryan

:+1: Happy to help!