cancel
Showing results for 
Search instead for 
Did you mean: 

A TaskListener to automatically assign a task if only one candidate user exists

smorring
Champ in-the-making
Champ in-the-making
I wanted to share the following TaskListener that I've used to handle the following situation:

A task is normally given some set of users as candidate users, but in some cases there is only one user in the candidate set.
In this case I'd like the one candidate user to automatically be the assignee of the task, so they don't have to claim it.

Following is the java code to accomplish this using a TaskListener.

/**
* Listener that automatically assigns a task if there is only one candidate.
*/
public class AutoAssigningTaskListener implements TaskListener
{
   @Override
   public void notify(DelegateTask t)
   {
      if(t.getAssignee() == null && t.getCandidates().size() == 1)
      {
         t.setAssignee(t.getCandidates().stream().filter(id-> id.getUserId() != null).findFirst().map(id -> id.getUserId()).orElse(null));
      }
   }
}

Feel free to indicate if there is a more preferred way to handle this situation.  Otherwise, I hope this is of use to someone.
1 REPLY 1

smorring
Champ in-the-making
Champ in-the-making
P.S.  I should also mention that this event is designed to work in response to the "create" event.

Following is the java code I use to register this listener:

private static ActivitiListener makeAutoAssigningTaskListener()
{
  ActivitiListener result = new ActivitiListener();
  result.setEvent(TaskListener.EVENTNAME_CREATE);
  result.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
  result.setImplementation("#{autoAssigningTaskListenerFactory.makeListener()}");
  return result;
}