Expand my Community achievements bar.

SOLVED

Workflow Launcher Compare Dates Condition

Avatar

Level 3

I'd like to create a workflow launcher based on dates. If cq:lastReplicated is newer than cq:lastModified, then kick off the workflow. Is this possible? I'm looking to trigger this only when a document has been published and it should not trigger when a document has been versioned (that's why I'm trying to add the date logic).

 

I've tried these settings:

 

Event Type: modified
Node type: dam:Asset
Path: /content/dam/mydocuments
Run modes: author
Conditions:

jcr:content/cq:lastReplicationAction==Activate
jcr:content/cq:lastReplicated>jcr:content/jcr:lastModified

 

Event Type: modified
Node type: dam:Asset
Path: /content/dam/mydocuments
Run modes: author
Conditions:

jcr:content/cq:lastReplicationAction==Activate
jcr:content/cq:lastReplicated-jcr:content/jcr:lastModified>=0

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@Gunars_V 

 

I would recommend using SampleReplicationPreprocessor or OnPublishEvent. Sharing samples for both.

 

SampleReplicationPreprocessor would act before publishing.

https://github.com/Adobe-Consulting-Services/acs-aem-samples/blob/master/core/src/main/java/com/adob...

 

OnPublishEvent will come into action after publishing 

https://aemsimplifiedbynikhil.wordpress.com/2022/10/20/automating-performing-tasks-on-replication-of... 

 

Workflow Laucher might not be able to deal with comparison operators like < & >


Aanchal Sikka

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

@Gunars_V 

 

I would recommend using SampleReplicationPreprocessor or OnPublishEvent. Sharing samples for both.

 

SampleReplicationPreprocessor would act before publishing.

https://github.com/Adobe-Consulting-Services/acs-aem-samples/blob/master/core/src/main/java/com/adob...

 

OnPublishEvent will come into action after publishing 

https://aemsimplifiedbynikhil.wordpress.com/2022/10/20/automating-performing-tasks-on-replication-of... 

 

Workflow Laucher might not be able to deal with comparison operators like < & >


Aanchal Sikka

Avatar

Community Advisor

Hi @Gunars_V ,

AEM Workflow Launchers do not support direct property comparisons, such as cq:lastReplicated > jcr:lastModified, as they only evaluate exact matches (==) or check for existence (!= null). Additionally, when an asset is published, cq:lastReplicated updates after the modification event, which can cause the launcher to misfire if the properties are not updated synchronously.

You can follow below approach to achieve this:

First Step: Trigger the workflow on publish using launcher. Modify the launcher configuration by setting the event type to modified, the node type to dam:Asset, and specifying the path as /content/dam/mydocuments, with run modes set to author. Use the condition jcr:content/cq:lastReplicationAction==Activate to ensure the workflow triggers only on publish events.

Second Step : Perform the date comparison logic inside a custom workflow step (Java code). Add a custom process step in your workflow model to programmatically compare cq:lastReplicated and jcr:lastModified.

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;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Calendar;

@Component(
    service = WorkflowProcess.class,
    property = {"process.label=Check Last Replicated vs Last Modified"}
)
public class CheckReplicationDateProcess implements WorkflowProcess {

    private static final Logger LOG = LoggerFactory.getLogger(CheckReplicationDateProcess.class);

    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) {
        ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class);

        if (resourceResolver != null) {
            String payloadPath = workItem.getWorkflowData().getPayload().toString();
            Resource resource = resourceResolver.getResource(payloadPath + "/jcr:content");

            if (resource != null) {
                ValueMap properties = resource.getValueMap();
                Calendar lastReplicated = properties.get("cq:lastReplicated", Calendar.class);
                Calendar lastModified = properties.get("jcr:lastModified", Calendar.class);

                if (lastReplicated != null && lastModified != null) {
                    if (lastReplicated.after(lastModified)) {
                        LOG.info("Triggering workflow as cq:lastReplicated > jcr:lastModified for {}", payloadPath);
                        // Continue workflow execution
                    } else {
                        LOG.info("Skipping workflow as lastModified is newer for {}", payloadPath);
                        workflowSession.terminateWorkflow(workItem.getWorkflow());
                    }
                }
            }
        }
    }
}

Regards,

Shiv Prakash

Avatar

Level 7

To create a workflow launcher based on cq:lastReplicated and cq:lastModified dates:

1. Configure the Workflow Launcher to trigger on the publish event (jcr:content/cq:lastReplicationAction == Activate).

2. Use a Custom Workflow Step for date comparison:
- Since Workflow Launchers can't handle < or >, implement a custom workflow step in Java to compare cq:lastReplicated and jcr:lastModified.
- In this step, compare the dates and decide whether to continue the workflow or terminate it.

Example Code for Custom Workflow Step:

public class CheckReplicationDateProcess implements WorkflowProcess {
    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) {
        Resource resource = ...; // Get resource from workItem
        Calendar lastReplicated = ...; // Get cq:lastReplicated
        Calendar lastModified = ...; // Get jcr:lastModified

        if (lastReplicated != null && lastModified != null && lastReplicated.after(lastModified)) {
            // Continue workflow
        } else {
            // Terminate workflow
        }
    }
}



Avatar

Administrator

@Gunars_V Did you find the suggestions helpful? Please let us know if you need more information. If a response worked, kindly mark it as correct for posterity; alternatively, if you found a solution yourself, we’d appreciate it if you could share it with the community. Thank you!



Kautuk Sahni