How to send an email when a usertask is assigned

“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 = “admin@example.org”;
  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(“noreply@camunda.org”);
            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:

Have fun with camunda 🙂

Getting Started

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

  • Monitoring Camunda Platform 7 with Prometheus

    Monitoring is an essential facet of running applications in a production system. Through this process, organizations collect and analyze data, and determine if a program is performing as expected within set boundaries. When combined with alerting, monitoring allows for detecting unexpected system behavior to mitigate exceptional situations as fast as possible. Furthermore, tracking the performance of a system enables organizations to improve those aspects that have the biggest impact with higher priority. One essential aspect of monitoring is the list of key metrics you want to observe. There are different categories of statistics that can be of interest here. To observe the defined metrics, there are plenty of application monitoring tools on the market today. They differ in many aspects...

    Read more
  • Securing Camunda 8 self-managed cluster and applications...

    Directory services are an effective way to manage an organization’s users, groups, printers, devices, and more. Most organizations accomplish this using Active Directory, Apache Directory, Oracle Internet Directory, or other similar tools. Recently I worked with a customer who wanted to see how he could secure the Camunda 8 Platform and process applications with such a directory. Their requirements consisted of: Allowing Directory users to access Camunda applications (Tasklist, Operate, Optimize) Accessing secured Tasklist & Operate APIs from our custom project Securing the custom project In this article, I’ll briefly explain the 3 easy steps taken to fulfill their requirements which include: Federate users from the Directory service into Keycloak Declare an application in Identity to access Camunda APIs Configure...

    Read more
  • Accelerate Connectivity with Camunda Platform 8.1

    We’re thrilled to announce Camunda Platform 8.1, the latest release of our process orchestration solution. This new version introduces features that accelerate connectivity to the many different systems and technologies that are required for true digital transformation, including: Create custom Connectors with our Integration Framework to accelerate connectivity New out-of-the-box Connectors for popular services Enhancements to Camunda Modeler that improve productivity Hot backups and official support for Amazon EKS and Red Hat OpenShift Plus, several upgrades requested by Camunda Platform 7 customers Organizations across all industries rely on complex technology stacks to adapt and enhance their operations in response to market dynamics, new disruptive companies, and increasing consumer expectations. Your technology stack likely includes everything from cutting-edge technologies to legacy...

    Read more

Ready to get started?

Still have questions?