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.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Servlet;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
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
 */
@Component(
  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());

 @Override
 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);

   log.info("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.

Conclusion

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.

Comments

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

    ReplyDelete

Post a Comment

Popular posts from this blog

Day 05: Working with Sling Servlets in AEM

AEM Developer Series Day 00: AEM Developer Series Day 01: Introduction to AEM Day 02: AEM Architecture Day 03: Setting up AEM Development Environment Day 04: Developing First OSGi Bundle Day 05: Working with Sling Servlets in AEM Day 06: Playing with Sling Post Servlet Day 07: Creating your first component in AEM Day 08: Dueling with JavaScript Use API Day 09: Dueling with Java User API Day 10: Getting to know Sling Models Day 11: Client Libraries in Action Day 12: Creating your custom OSGi Configuration Day 13: Schedulers in AEM Day 14: Eventing in AEM Day 15: Custom Workflows in AEM Day 16: Creating JMX Beans in AEM Day 17: Working with QueryBuilder API Day 18: Working with Granite Datasources in AEM Day 19: Replication API in Action Day 20: Working with Users and Groups in AEM A Servlet is a class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. For such applications, Servlet tec

Day 00: AEM Developer Series

image source:  https://solutionpartners.adobe.com/ 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

Day 12: Creating your Custom OSGi configuration

AEM Developer Series Day 00: AEM Developer Series Day 01: Introduction to AEM Day 02: AEM Architecture Day 03: Setting up AEM Development Environment Day 04: Developing First OSGi Bundle Day 05: Working with Sling Servlets in AEM Day 06: Playing with Sling Post Servlet Day 07: Creating your first component in AEM Day 08: Dueling with JavaScript Use API Day 09: Dueling with Java User API Day 10: Getting to know Sling Models Day 11: Client Libraries in Action Day 12: Creating your custom OSGi Configuration Day 13: Schedulers in AEM Day 14: Eventing in AEM Day 15: Custom Workflows in AEM Day 16: Creating JMX Beans in AEM Day 17: Working with QueryBuilder API Day 18: Working with Granite Datasources in AEM Day 19: Replication API in Action Day 20: Working with Users and Groups in AEM We all know that AEM works on Apache Felix  which is an implementation of OSGi . OSGi provides a way to manage bundles and configurations. We saw one example of an OSGi configurati