1

I am trying to send an email to the contact owner when the email on contact has bounced. Below is the code, it works and send email to the contact if I remove the IsEmail Bounced condition.

trigger testTrigger on Contact(after update) {
    public Emailtemplate EmailBounced;
    EmailBounced = [select id, name from EmailTemplate where name = 'Email Bounce mail template'];
    for (Contact con: Trigger.New) {
        if (con.email != null && con.isemailBounced == TRUE) {
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setTargetObjectId(con.ownerId);
            email.setsaveAsActivity(FALSE);
            email.setTemplateId(EmailBounced.id);
            email.setSubject('Email Bounced Alert : Email does not exist');
            if (con.ownerId != null) {
                Messaging.sendEmail(
                        new Messaging.SingleEmailMessage[] {email});
                system.debug('email sent ---------');
            }
        }
    }
}

Tried with the workflow but not luck.

  • Make sure that you have enable bounce management feature in your org. After that check your code. To verify update a contact with an invalid email address that you would know bounce email and verify your code. – Mahmood Oct 27 '16 at 19:48
  • the flag is turning TRUE, even though it is not invoking the trigger. – apple123 Oct 27 '16 at 20:50
  • put a system.debug before the "if" statement where it is failing to check the values of bounce email field and also verify if the contact has email address. – Mahmood Oct 27 '16 at 21:14
  • 1
    you may be subject to this: developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – cropredy Oct 27 '16 at 21:23
  • how does isemailBounced__c get set? this is a custom field. How is it related to OOB field IsEmailBounced ? – cropredy Oct 28 '16 at 1:15
3

@sfdcfox covers this in an 2011 post in the Developer Forum that describes how bounces don't cause normal DML events on objects and hence triggers won't execute.

He suggests the following workaround (which makes sense as when a bounce occurs is asynchronous and hence when the notification occurs to the contact owner can be asynchronous as well)

  • Write an Apex Scheduled Job (run daily)
  • That queries for Contacts where EmailBouncedDate = YESTERDAY
  • And then sets/updates a custom field on Contact such as isReadyForBounceNotification__c to true. This update will be a DML event ..
  • That then is picked up by a workflow that sends out an email alert and finally resets the isReadyForBounceNotification__c to false.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.