How to send an email when a usertask is assigned

By
  • Blog
  • >
  • How to send an email when a usertask is assigned
TOPICS

30 Day Free Trial

Bring together legacy systems, RPA bots, microservices and more with Camunda

Sign Up for Camunda Content

Get the latest on Camunda features, events, top trends, and more.

TRENDING CONTENT

“I don’t want to watch my camunda Tasklist all day. When there is something to do, I want to get an email containing a link that takes me directly to the respective task!”

This is a typical statement by process participants (aka business users). There are numerous possible ways to implement such a requirement in your process application, e.g. using a service task:

example process

 

But this is not really appropriate, since it makes the process model rather verbose and therefore less valuable for business-IT-alignment.

The more elegant way is to use a task listener: This very powerful concept is described in detail in the userguide, and the behavior we want is a perfect use case, so let’s see how it works!

Let’s say we created a simple process application (like in the Get Started Tutorial), but this process contains only one usertask. The next step is to created a Task Listener Class like the one below. The important parts are:

  • It implements the Task Listener Interface.
  • It retrieves the assignee from the Task Object and loads the according User Profile including the assignee’s email address.
  • It creates a simple mail containing a deep link to the task instance.

package org.camunda.bpm.quickstart;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.SimpleEmail;
import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.delegate.DelegateTask;
import org.camunda.bpm.engine.delegate.TaskListener;
import org.camunda.bpm.engine.identity.User;
import org.camunda.bpm.engine.impl.context.Context;

public class TaskAssignmentListener implements TaskListener {

  // TODO: Set Mail Server Properties
  private static final String HOST = “mail.example.org”;
  private static final String USER = “[email protected]”;
  private static final String PWD = “toomanysecrets”;

  private final static Logger LOGGER = Logger.getLogger(TaskAssignmentListener.class.getName());

  public void notify(DelegateTask delegateTask) {

    String assignee = delegateTask.getAssignee();
    String taskId = delegateTask.getId();

    if (assignee != null) {
   
      // Get User Profile from User Management
      IdentityService identityService = Context.getProcessEngineConfiguration().getIdentityService();
      User user = identityService.createUserQuery().userId(assignee).singleResult();

      if (user != null) {
     
        // Get Email Address from User Profile
        String recipient = user.getEmail();
     
        if (recipient != null && !recipient.isEmpty()) {

          Email email = new SimpleEmail();
          email.setHostName(HOST);
          email.setAuthentication(USER, PWD);

          try {
            email.setFrom(“[email protected]”);
            email.setSubject(“Task assigned: ” + delegateTask.getName());
            email.setMsg(“Please complete: https://localhost:8080/camunda/app/tasklist/default/#/task/” + taskId);


            email.addTo(recipient);


            email.send();
            LOGGER.info(“Task Assignment Email successfully sent to user ‘” + assignee + “‘ with address ‘” + recipient + “‘.”);          

          } catch (Exception e) {
            LOGGER.log(Level.WARNING, “Could not send email to assignee”, e);
          }

        } else {
          LOGGER.warning(“Not sending email to user ” + assignee + “‘, user has no email address.”);
        }

      } else {
        LOGGER.warning(“Not sending email to user ” + assignee + “‘, user is not enrolled with identity service.”);
      }

    }

  }

}

The last step is to assign the class as a task listener to the usertask in the process model. As you can see below, the task listener will be executed when the usertask has been assigned:

assigning the class as a task listener to the usertask in the process model

And that’s about it! If you want to see an example ready to run, just check out the Task Assignment Email Quickstart.

From a user perspective, it will look like this:

camunda taskAssignment

Have fun with camunda 🙂

Getting Started

Getting started on Camunda is easy thanks to our robust documentation and tutorials

Try All Features of Camunda

Related Content

We're streamlining Camunda product APIs, working towards a single REST API for many components, simplifying the learning curve and making installation easier.
Learn about our approach to migration from Camunda 7 to Camunda 8, and how we can help you achieve it as quickly and effectively as possible.
We've been working hard to reduce the job activation latency in Zeebe. Read on to take a peek under the hood at how we went about it and then verified success.