Highlighted

Invoking a workflow from Servlet in AEM 6.5.5 | AEM Community Blog Seeding

Avatar

Avatar

kautuk_sahni

Community Manager

Total Posts

5.6K

Likes

961

Correct Answer

1.1K

Avatar

kautuk_sahni

Community Manager

Total Posts

5.6K

Likes

961

Correct Answer

1.1K
kautuk_sahni
Community Manager

15-11-2020

BlogImage.jpg

Invoking a workflow from Servlet in AEM 6.5.5 by AEM Queries & Solutions

Abstract

Here is an article on how we can implement a requirement where we want to automate the initiation of the AEM workflows in any servlet with some piece of code. So here we will see how to locate the workflow in the CRX and using the same path we can start the workflow with the use workflow session.

So to start with we will decide which workflow we want to initiate in the servlet. Lets go to http://localhost:4502/libs/cq/workflow/content/console.html and lets say we will automate Download Asset workflow.

So here if we want know the model of this workflow then the value given in the ID column will be our workflow model path.

So now in our Servlet we need payload path and the workflow model. payload path is the path to the node which we want to run workflow on.

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.Servlet;
import javax.servlet.ServletException;

import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.json.JSONException;
import org.json.JSONObject;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowService;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.model.WorkflowModel;
import com.drew.lang.StringUtil;


@Component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "= Invoke Workflow Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=" + "/bin/invoke/workflow" })
public class invokeWorkflowServlet extends SlingAllMethodsServlet {

/** Default log. */
protected final transient Logger log = LoggerFactory.getLogger(this.getClass());

private static final long serialVersionUID = 1L;

@Reference
private transient WorkflowService workflowService;

@Reference
private transient ResourceResolverFactory resolverFactory;

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {

Session session;


Map<String, Object> param = new HashMap<>();
param.put(ResourceResolverFactory.SUBSERVICE, "writeSystemUser");
ResourceResolver resolver = null;


WorkflowSession wfSession = workflowService.getWorkflowSession(session);
WorkflowModel wfModel = wfSession.getModel("/var/workflow/models/dam/dam_download_asset");
WorkflowData wfData = wfSession.newWorkflowData("JCR_PATH", "/content/dam/we-retail/en/features/cart.png");
wfSession.startWorkflow(wfModel, wfData);

session.save();
session.logout();



}

}

Read Full Blog

Invoking a workflow from Servlet in AEM 6.5.5

Q&A

Please use this thread to ask the related questions.

AEM AEMEBlogSeeding Experience Manager