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.
SOLVED

Send email notification to business when page gets published not on modified AEMaaCS

Avatar

Level 2

AEMaaCS - AEM launcher is not working because it is getting initiated for the first replication and it is not sending any notification on next times.
So I tried using Event handler (EVENT.TOPIC), it is sending the notification to the business. But problem here is it is triggering email multiple times(2-3 times).
Onevent  it is triggering the workflow where it has Messagegatewayservice with all hardcoded input(straight forward htmlemail value passing to the message service)
Can you please help me on this? Thanks in Advance
Event Handler:

dmanibabu_0-1709184285816.png

dmanibabu_1-1709184352041.png

Workflow:

dmanibabu_2-1709184441967.png

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi, the workflow model you will create on page publish will get trigger every time you publish the page, 
So to differentiate the first page publish, you can use an identifier. I have summarized the steps below.
1. Create a custom workflow model that gets triggered on page publish.

2. Create a workflow process class as you have created above

 

(
    service = WorkflowProcess.class,
    property = {
        "process.label=New Page Publish Email Notification"
    }
)
public class NewPagePublishEmailWorkflowProcess implements WorkflowProcess {

    
    private MessageGatewayService messageGatewayService;

    
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
        String pagePath = workItem.getWorkflowData().getPayload().toString();

        ResourceResolver resolver = workflowSession.adaptTo(ResourceResolver.class);

        Page page = resolver.getResource(pagePath).adaptTo(Page.class);

        // Check if the page is being published for the first time
        if(!page.getProperties().containsKey("mailTriggered")){
        	sendEmail();
        	page.getProperties().put("mailTriggered", false);
        	resolver.commit();
        }
    }

    private void sendEmail() {
        try {
            Email email = new SimpleEmail();
            email.addTo("business@example.com");
            email.setSubject("A new page has been published");
            email.setMsg("A new page has been published on the website.");

            MessageGateway<Email> messageGateway = messageGatewayService.getGateway(Email.class);
            messageGateway.send(email);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

 3. Configure the workflow model with above Workflow process class.

View solution in original post

11 Replies

Avatar

Community Advisor

@dmanibabu You can try below code snippet in case of page publish. But, launcher has one problem as it gets call multiple times. In place launcher we can call event listener to listen particular event on property value modified. It will also allow us to get topic as replication with resource.

From listener we can call our send email workflow model.   

 

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0"
    jcr:primaryType="cq:WorkflowLauncher"
    condition="cq:lastReplicated"
    conditions="[]"
    description="\0"
    disabledFeatures="[]"
    enabled="{Boolean}true"
    eventType="{Long}16"
    excludeList="\0"
    features="[]"
    glob="/content/practice"
    nodetype="cq:PageContent"
    runModes="author,publish"
    workflow="/var/workflow/models/send-mial-workflow"/>

 

Avatar

Level 2

@Imran__Khan As you said launcher is triggering the workflow only for the first time of page publish. it is not looking for second time when page is published.
Our requirement is whenever page is published under specific path, we have to send mail to business.
Event handler I am using it to look for event . For event listener, do you have any reference code snippet. I am not sure listener wont trigger workflow multiple times. 

Avatar

Community Advisor

Please follow below links on the same. It talks about event listeners and email template step by step. Code snippet is also available to consume.
https://javadoubts.com/aem-event-listener/

https://javadoubts.com/aem-email-custom-template/

Avatar

Level 2

@arunpatidar Tried this but still I am receiving same email more than 3times. Not sure why Mail is triggering multiple times.

Avatar

Community Advisor

Hi,
Can you please check how many events are triggered when you publish the page?

http://localhost:4502/system/console/events

 



Arun Patidar

Avatar

Community Advisor

@dmanibabu Agree, it will get call multiple times. Please apply check resource path and resource type in Java class for which we want to send email. This will make sure email get send one time. 

Checking property and node type will not help here as while page publishing it will also publish other references content.

Avatar

Level 2

@Imran__Khan yes I do have the resource path after the replication action, but still it is getting multiple times. please refer screenshot

dmanibabu_0-1709528483176.pngdmanibabu_1-1709528490896.png

 

@arunpatidar I tried in local but problem here is I don't have mail setup in my local. So I am trying it in development instance. In local it is triggering multiple times because of missing mail config.

dmanibabu_2-1709528627486.png

 

Also the job is keep in retrying it, till it get success. 

dmanibabu_0-1709529824275.png

can we make this to execute only once?

 

Avatar

Correct answer by
Community Advisor

Hi, the workflow model you will create on page publish will get trigger every time you publish the page, 
So to differentiate the first page publish, you can use an identifier. I have summarized the steps below.
1. Create a custom workflow model that gets triggered on page publish.

2. Create a workflow process class as you have created above

 

(
    service = WorkflowProcess.class,
    property = {
        "process.label=New Page Publish Email Notification"
    }
)
public class NewPagePublishEmailWorkflowProcess implements WorkflowProcess {

    
    private MessageGatewayService messageGatewayService;

    
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
        String pagePath = workItem.getWorkflowData().getPayload().toString();

        ResourceResolver resolver = workflowSession.adaptTo(ResourceResolver.class);

        Page page = resolver.getResource(pagePath).adaptTo(Page.class);

        // Check if the page is being published for the first time
        if(!page.getProperties().containsKey("mailTriggered")){
        	sendEmail();
        	page.getProperties().put("mailTriggered", false);
        	resolver.commit();
        }
    }

    private void sendEmail() {
        try {
            Email email = new SimpleEmail();
            email.addTo("business@example.com");
            email.setSubject("A new page has been published");
            email.setMsg("A new page has been published on the website.");

            MessageGateway<Email> messageGateway = messageGatewayService.getGateway(Email.class);
            messageGateway.send(email);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

 3. Configure the workflow model with above Workflow process class.

Avatar

Administrator

@dmanibabu 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