Skip to main content

Workflows in AEM 05 - Trigger Workflow Programmatically

It is often needed to trigger a workflow programmatically. Hence, in this small post, we will see how we can easily do that.

package org.redquark.demo.core.servlets;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Servlet;

import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.model.WorkflowModel;

 * @author Anirudh Sharma
  service = Servlet.class, 
  property = { 
    Constants.SERVICE_DESCRIPTION + "=Trigger Worklow Servlet",
    "sling.servlet.methods=" + HttpConstants.METHOD_GET, 
    "sling.servlet.paths=" + "/bin/triggerWorkflow" 
public class TriggerWorkflowServlet extends SlingSafeMethodsServlet {

 private static final long serialVersionUID = 4235730140092282985L;

 private final Logger log = LoggerFactory.getLogger(this.getClass());

 protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {

  try {
   final String payloadPath = "/content/we-retail/language-masters/en/equipment";

   // Getting the resource resolver
   final ResourceResolver resolver = request.getResourceResolver();

   // Get the workflow session from the resource resolver
   final WorkflowSession workflowSession = resolver.adaptTo(WorkflowSession.class);

   // Workflow model path - This is the already created workflow
   final String model = "/var/workflow/models/metadata_map_example";

   // Get the workflow model object
   final WorkflowModel workflowModel = workflowSession.getModel(model);

   // Create a workflow Data (or Payload) object pointing to a resource via JCR
   // Path (alternatively, a JCR_UUID can be used)
   final WorkflowData workflowData = workflowSession.newWorkflowData("JCR_PATH", payloadPath);

   // Optionally pass in workflow metadata via a Map
   final Map<String, Object> workflowMetadata = new HashMap<>();
   workflowMetadata.put("anyData", "You Want");
   workflowMetadata.put("evenThingsLike", new Date());

   // Start the workflow!
   workflowSession.startWorkflow(workflowModel, workflowData, workflowMetadata);"Workflow: {} started", model);
   response.getWriter().println("Workflow Exxecuted");

  } catch (WorkflowException | IOException e) {
   log.error(e.getMessage(), e);


Following things are needed for this -

  • Payload path - the payload on which we wish to run the workflow
  • Workflow session - this is taken from the Resource Resolver.
  • Workflow model - the path of the workflow model that is going to be run on the payload.
  • Workflow data - the data (if any) that we are needed to pass in the workflow.
  • In the end, we start the workflow.


So, we saw that it is pretty easy to trigger a workflow programmatically. This post shows how to run a workflow from a servlet but we can easily use this code in a service/component/slingmodel etc.

You can find the code to this in my GitHub repository.

I hope you enjoyed this post. Your suggestions and feedback are always welcome. Feel free to befriend me on FacebookTwitter or Linked In or say Hi by email.


  1. @Anirudh You are genius.. This really helped.
    Thanks a lot.... :)


Post a Comment

Popular posts from this blog

Day 00: AEM Developer Series

Hello everyone! Welcome to this AEM development series. We can all see the revolution of Digital Marketing today. Companies are dying to be a part of it and they have made this a war for the Digital Marketing tools.
Adobe is way ahead in this war and has gained a lot of market capture. They are leaders in the Digital Marketing platforms since 2014-15. One of the flagship product in Adobe's Digital Marketing suite is Adobe Experience Manager (AEM).
Since AEM is in huge demand, the people who know how to develop on AEM are also in huge demand. But developing on AEM is not easy as it is made up of various open-source technologies such as Apache Felix (OSGi), Apache Sling, Apache Oak and Adobe's own technologies like Granite, HTL etc. Learning all these technologies in conjunction can sometimes become confusing and frustrating 😫.
When I first started learning AEM in 2016, I was dumbfounded to see there is so much going on under the hood. I then spent months to gather all the res…

Day 05: Working with Sling Servlets in AEM

Day 03: Setting up AEM Development Environment