April 10, 2020
Estimated Post Reading Time ~

Custom Workflows in AEM

A workflow enables us to automate a wide range of activities in AEM. A workflow consists of steps that are executed in a specific order. Each step performs a distinct activity such as activating a page or sending an email message.

Workflows can interact with assets in the repository, user accounts, and services. Therefore, workflows can coordinate complicated activities that involve any aspect of AEM.

Many useful workflow models are provided out of the box with AEM. In addition, any number of custom workflow models, tailored to the specific needs of our project, can be also be created.

In this post, we will be creating a custom workflow that will read the user passed data and logs it. So, without wasting much time, let's dive into the code.

Custom Workflow

  • To create a custom workflow component, go to your project folder under /apps in CRXDE and create a component with the following configuration.
Create a component
  • Now create cq:dialog node with the following configuration -
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    jcr:primaryType="nt:unstructured"
    jcr:title="Custom Workflow"
    sling:resourceType="cq/gui/components/authoring/dialog">
    <content
        jcr:primaryType="nt:unstructured"
        sling:resourceType="granite/ui/components/foundation/container">
        <layout
            jcr:primaryType="nt:unstructured"
            sling:resourceType="granite/ui/components/foundation/layouts/tabs"
            type="nav"/>
        <items jcr:primaryType="nt:unstructured">
            <tab
                jcr:primaryType="nt:unstructured"
                jcr:title="Properties"
                sling:resourceType="granite/ui/components/foundation/section">
                <layout
                    jcr:primaryType="nt:unstructured"
                    sling:resourceType="granite/ui/components/foundation/layouts/fixedcolumns"/>
                <items jcr:primaryType="nt:unstructured">
                    <columns
                        jcr:primaryType="nt:unstructured"
                        sling:resourceType="granite/ui/components/foundation/container">
                        <items jcr:primaryType="nt:unstructured">
                            <text
                                jcr:primaryType="nt:unstructured"
                                sling:resourceType="granite/ui/components/foundation/form/textfield"
                                fieldDescription="Please enter title"
                                fieldLabel="Title"
                                name="./metaData/textValue"/>
                            <date
                                jcr:primaryType="nt:unstructured"
                                sling:resourceType="granite/ui/components/coral/foundation/form/datepicker"
                                class="field"
                                fieldDescription="Enter the date"
                                fieldLabel="Date"
                                name="./metaData/dateValue"
                                type="datetime"/>
                        </items>
                    </columns>
                </items>
            </tab>
        </items>
    </content>
</jcr:root>
  • Now create a new node cq:ediConfig of type cq:EditConfig underneath workflowDemo node with the following configuration - 
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    cq:dialogMode="floating"
    cq:inherit="{Boolean}true"
    jcr:primaryType="cq:EditConfig">
    <cq:formParameters
        jcr:description="This workflow gets the user input and logs them"
        jcr:primaryType="nt:unstructured"
        jcr:title="Custom Workflow"
        PROCESS="org.redquark.demo.core.workflow.CustomWorkflow"
        PROCESS_AUTO_ADVANCE="true"/>
</jcr:root>
  • The PROCESS value is the name of the class that will have Java backend logic.
  • Create a java class named CustomWorkflow and paste the following code in it.
package org.redquark.demo.core.workflows;

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.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;

/**
 * @author Anirudh Sharma
 * 
 * Custom Workflow Step
 */
@Component(service = WorkflowProcess.class, property = {
 "process.label = Custom Workflow"
})
public class CustomWorkflow implements WorkflowProcess {

 /**
  * Logger
  */
 private static final Logger log = LoggerFactory.getLogger(CustomWorkflow.class);

 /**
  * Overridden method which executes when the workflow is invoked
  */
 @Override
 public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {

  log.info("Executing the workflow");

  try {

   String textValue = metaDataMap.get("textValue", "empty");

   String dateValue = metaDataMap.get("dateValue", "empty");

   log.info("Text: {}", textValue);
   log.info("Date: {}", dateValue);

  } catch (Exception e) {

   log.error(e.getMessage(), e);
  }
 }

}
  • Here we are implementing the WorkflowProcess interface and defining the process.label property which defines the name of the workflow step.
  • Deploy the code on your AEM server.
  • Navigate to http://<host>:<post>/libs/cq/workflow/admin/console/content/models.html and click on Create >> Create Model and enter the Title of the workflow as below and configure it as per the below screenshots
Create a workflow model
Edit the workflow model
Add the steps in the workflow
  • Drag and drop the steps from the side rail and double click on the Custom Workflow step.
  • Go to the Properties tab and enter sample values and save the dialog. Now click on Sync.
  • Now go to sites console and run the workflow as per below screenshots.
Create a workflow on page
Select Workflow Model
Create
Inbox notifications
Complete the workflow step
  • After completing the workflow, you will see the following traces in logs

17.10.2018 20:30:51.869 *INFO* [JobHandler: /var/workflow/instances/server0/2018-10-17_1/customWorkflow_11:/content/we-retail/language-masters/en/women] org.redquark.demo.core.workflows.CustomWorkflow Executing the workflow
17.10.2018 20:30:51.874 *INFO* [JobHandler: /var/workflow/instances/server0/2018-10-17_1/customWorkflow_11:/content/we-retail/language-masters/en/women] org.redquark.demo.core.workflows.CustomWorkflow Text: Custom Workflow Testing Demo
17.10.2018 20:30:51.874 *INFO* [JobHandler: /var/workflow/instances/server0/2018-10-17_1/customWorkflow_11:/content/we-retail/language-masters/en/women] org.redquark.demo.core.workflows.CustomWorkflow Date: 2018-10-17T19:17:00.000+05:30


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.