Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

Complete Workflow Programatically | AEM 6.5

Avatar

Employee

Hi All,

I have a workflow which activates the page based on a condition and there are some additional process also available apart from activation. In the below code, I am replicating the asset , if the drop down selection is approve(I am using dialog in the dynamic participant step). Now how can I complete the workflow programatically. I cannot do this using OR split options as the split is not suitable for my use case.

String payLoad = workItem.getWorkflowData().getPayload().toString();
List<HistoryItem> history = workflowSession.getHistory(workItem.getWorkflow());
if (!history.isEmpty()) {
HistoryItem last = history.get(history.size() - 1);
String action = last.getWorkItem().getMetaDataMap().get("revieweroptions", StringUtils.EMPTY);
if (StringUtils.equalsIgnoreCase("approve", action)) {
try {
replicator.replicate(workflowSession.adaptTo(Session.class), ReplicationActionType.ACTIVATE, payLoad);
} catch (ReplicationException e) {
logger.error("Unable to replicate the asset {}", payLoad);
}

}
if(StringUtils.equalsIgnoreCase("reject", action)) {
// Reject and Send an email
}
5 Replies

Avatar

Level 5

Hi @SateeshRe ,

Your if conditions should finish the Process Step once they are executed.

To end the Workflow however you need to have an END step at the end and your Process step should have  PROCESS_AUTO_ADVANCE="true"

It should look something like this

 

 

<nodes jcr:primaryType="nt:unstructured">
        <node0
            jcr:primaryType="cq:WorkflowNode"
            title="Start"
            type="START">
            <metaData jcr:primaryType="nt:unstructured"/>
        </node0>
        <node1
            jcr:primaryType="cq:WorkflowNode"
            description="Your Workflow Process"
            title="Your Workflow Process"
            type="PROCESS">
            <metaData
                jcr:primaryType="nt:unstructured"
                PROCESS="com.your.process.step.YourJavaClassName"
                PROCESS_AUTO_ADVANCE="true"/>
        </node1>
        <node2
            jcr:primaryType="cq:WorkflowNode"
            title="End"
            type="END">
            <metaData jcr:primaryType="nt:unstructured"/>
        </node2>
    </nodes>

 

 

 You need not use an OR split as you can end the workflow with either activation or sending the rejection email. Hope it helps.

Avatar

Employee

Thanks for the reply.

 

It is not working as expected, It is still going to the next step and executing the dynamic participant step.

SateeshRe_0-1711512387854.png

 

Avatar

Level 5

Then you need to change the design of your workflow, now you do need an or Split. 
Currently , you are asking the workflow to move to the next step but you need to have a condition when it should move to Dynamic Step, the other one should point to the END node,

Avatar

Employee

OR split will not really help here because I need to have a approve/reject and select a reviewer options. If I go with OR split, it will not allow me to choose the reviewer using dialog.

Avatar

Level 6

Hi @SateeshRe if your main question is: How can you complete the workflow programmatically after the asset replication or rejection?

Then ,

  • To complete the workflow programmatically, you can use the AEM Workflow API.
  • Here’s an example of how you can achieve this:
String payLoad = workItem.getWorkflowData().getPayload().toString();
List<HistoryItem> history = workflowSession.getHistory(workItem.getWorkflow());
if (!history.isEmpty()) {
    HistoryItem last = history.get(history.size() - 1);
    String action = last.getWorkItem().getMetaDataMap().get("revieweroptions", StringUtils.EMPTY);
    if (StringUtils.equalsIgnoreCase("approve", action)) {
        try {
            replicator.replicate(workflowSession.adaptTo(Session.class), ReplicationActionType.ACTIVATE, payLoad);
        } catch (ReplicationException e) {
            logger.error("Unable to replicate the asset {}", payLoad);
        }
        sendApprovalEmail(payLoad);
        workflowSession.complete(workItem, "approvedRouteName", null); // Use the correct route name for approval
    } else if (StringUtils.equalsIgnoreCase("reject", action)) {
        sendRejectionEmail(payLoad);
        workflowSession.complete(workItem, "rejectedRouteName", null); // Use the correct route name for rejection
    } else {
        logger.warn("Unknown or unsupported action: {}", action);
    }
} else {
    // Handle null history
    logger.error("History is empty");
}
private void sendApprovalEmail(String payload) {
    // Implement email sending logic for approval
}
private void sendRejectionEmail(String payload) {
    // Implement email sending logic for rejection
}

Hope this helps !