Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

AEM Workflow - Conditionally trigger on lastReplicationAction

Avatar

Level 2

Hello there,

I have a workflow that conditionally will be triggered once "cq:lastReplicationAction == Activate"

Issue is:


this property is stored with this value, and therefore does not represent exclusively when the page was published. therefore any other action such as submitting a dialog will also trigger the workflow.

 

Is there any way to solve this such as cleaning the property or verifying another property?

 

thanks in advance!

Topics

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @caradeotario 

If you are concern to clearing cq:lastReplicationAction property after the workflow execution, you can consider using a custom flag or marker property instead. This approach involves setting a custom property on the page node to indicate that the workflow has been triggered and executed successfully. Here's how you can implement this alternative approach:

  1. Add a Custom Flag Property:

    • Introduce a custom boolean property (e.g., workflowTriggered) on the page node in the JCR repository. This property will serve as a flag to indicate whether the workflow has been triggered and executed successfully.
  2. Set the Flag Property in Workflow:

    • Within your workflow execution logic, set the value of the workflowTriggered property to true on the page node after the workflow has been successfully executed.
  3. Check Flag Property in Workflow Model:

    • Modify your workflow model to include an additional condition that checks the value of the workflowTriggered property before executing the workflow steps. Proceed with the workflow execution only if this property indicates that the workflow has not been triggered before.
  4. Implementation:

    • You can implement the setting of the workflowTriggered property using Java code similar to the previous example. Instead of clearing the cq:lastReplicationAction property, set the value of the custom flag property (workflowTriggered) to true.

Here's a simplified example of how you can set the custom flag property using Java code within an AEM workflow step:

 

@Component(service = WorkflowProcess.class, property = {"process.label=Set Workflow Triggered Flag"})
public class SetWorkflowTriggeredFlagProcess implements WorkflowProcess {

    private static final Logger LOGGER = LoggerFactory.getLogger(SetWorkflowTriggeredFlagProcess.class);

    @Reference
    private ResourceResolverFactory resourceResolverFactory;

    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
        WorkflowData workflowData = workItem.getWorkflowData();
        String path = workflowData.getPayload().toString();

        try (ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(null)) {
            Session session = resourceResolver.adaptTo(Session.class);
            Node node = session.getNode(path);

            // Set the custom flag property 'workflowTriggered' to true
            node.setProperty("workflowTriggered", true);
            session.save();
            LOGGER.info("Successfully set 'workflowTriggered' flag for node: {}", path);
        } catch (RepositoryException e) {
            LOGGER.error("RepositoryException occurred: {}", e.getMessage());
            throw new WorkflowException("RepositoryException occurred", e);
        }
    }
}

 

 

View solution in original post

9 Replies

Avatar

Community Advisor

Hi @caradeotario 

 

You can go through this and it may help you. You can address this issue by incorporating additional checks or properties to ensure that the workflow is triggered only when the page is activated, rather than any other action such as submitting a dialog. Here are a few approaches you can consider:

  1. Custom Workflow Launcher:

    • In AEM, you can create custom workflow launchers using OSGi configurations or AEM Workflow Launcher API. This launcher can be configured to trigger the workflow only when specific conditions are met, such as when a page is activated. You can implement custom logic to determine the activation status of the page before triggering the workflow.
  2. Custom Metadata Property:

    • Introduce a custom metadata property to the page node in the JCR repository. This property can indicate the publication status of the page (e.g., "Published" or "Draft"). Update this property whenever the page is activated or deactivated. Configure the workflow launcher to trigger the workflow only when this custom property meets certain criteria.
  3. Additional Conditions in Workflow Model:

    • Modify the workflow model in AEM to include additional conditions that validate whether the activation action is indeed a publication action. For example, you can add conditions that check if the page has changed since the last activation or if specific metadata fields related to publication are set.
  4. Event Listener:

    • Implement a custom event listener using AEM's Event Handler API. This listener can listen for specific events related to page activation. When such an event occurs, the listener can execute custom logic to determine if the activation action warrants triggering the workflow.
  5. Cleanup Mechanism:

    • Implement a scheduled job or workflow process in AEM that periodically checks and cleans up the cq:lastReplicationAction property or any other relevant properties. This ensures that the property accurately reflects the page's publication status and reduces the likelihood of false triggers.

Choose the approach that best suits your requirements and environment. It's often a good practice to combine multiple strategies for robustness and flexibility. Additionally, thoroughly test any changes to ensure they behave as expected in your AEM instance.

Avatar

Level 2

thanks for your answer.
already have a custom workflow launcher:
Screenshot 2024-02-18 at 14.40.27.png
how can I achieve as you suggested, a double check on my workflow model in order to don't get false triggers. should I clear the field? If so is that a good practice and how do I do it?

Avatar

Correct answer by
Community Advisor

Hi @caradeotario 

If you are concern to clearing cq:lastReplicationAction property after the workflow execution, you can consider using a custom flag or marker property instead. This approach involves setting a custom property on the page node to indicate that the workflow has been triggered and executed successfully. Here's how you can implement this alternative approach:

  1. Add a Custom Flag Property:

    • Introduce a custom boolean property (e.g., workflowTriggered) on the page node in the JCR repository. This property will serve as a flag to indicate whether the workflow has been triggered and executed successfully.
  2. Set the Flag Property in Workflow:

    • Within your workflow execution logic, set the value of the workflowTriggered property to true on the page node after the workflow has been successfully executed.
  3. Check Flag Property in Workflow Model:

    • Modify your workflow model to include an additional condition that checks the value of the workflowTriggered property before executing the workflow steps. Proceed with the workflow execution only if this property indicates that the workflow has not been triggered before.
  4. Implementation:

    • You can implement the setting of the workflowTriggered property using Java code similar to the previous example. Instead of clearing the cq:lastReplicationAction property, set the value of the custom flag property (workflowTriggered) to true.

Here's a simplified example of how you can set the custom flag property using Java code within an AEM workflow step:

 

@Component(service = WorkflowProcess.class, property = {"process.label=Set Workflow Triggered Flag"})
public class SetWorkflowTriggeredFlagProcess implements WorkflowProcess {

    private static final Logger LOGGER = LoggerFactory.getLogger(SetWorkflowTriggeredFlagProcess.class);

    @Reference
    private ResourceResolverFactory resourceResolverFactory;

    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
        WorkflowData workflowData = workItem.getWorkflowData();
        String path = workflowData.getPayload().toString();

        try (ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(null)) {
            Session session = resourceResolver.adaptTo(Session.class);
            Node node = session.getNode(path);

            // Set the custom flag property 'workflowTriggered' to true
            node.setProperty("workflowTriggered", true);
            session.save();
            LOGGER.info("Successfully set 'workflowTriggered' flag for node: {}", path);
        } catch (RepositoryException e) {
            LOGGER.error("RepositoryException occurred: {}", e.getMessage());
            throw new WorkflowException("RepositoryException occurred", e);
        }
    }
}

 

 

Avatar

Community Advisor

Hi @caradeotario 
Use both 

"cq:lastReplicationAction == Activate" and cq:lastReplicated in order to trigger this.

 

"cq:lastReplicationAction" won't but cq:lastReplicated be changing for subsequent activation. 



Arun Patidar

Avatar

Level 2

hello @arunpatidar 
thats a good approach but since the value of cq:lastReplicated is a timestamp, what do I compare in order to check this?

Avatar

Community Advisor

In your scenario, best way is to use workflow Launchers. Follow below link to understand Launchers.

https://medium.com/@toimrank/aem-launcher-3358aef72d8c

Avatar

Level 4

@caradeotario  you can add a condition to check the last modified date and replication date. 

Avatar

Administrator

@caradeotario Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni