function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RecondoRecondo 

Trigger Notification for Deleted Records

Hi, I am working on setting up an email notification to be sent once an account record is deleted. I have looked into workflows but it would seem a trigger is a better avenue of approach. I am still learning how to write triggers so am not 100% sure how to leverage once a record is deleted. Any suggestions would be great. Thanks!

 

Best Answer chosen by Admin (Salesforce Developers) 
CheyneCheyne

You are right that you need to use a trigger, as workflows cannot be triggered off of deleted records. Assuming you are looking to send an email containing the account name, each time an account is deleted, you could try something like this (untested):

 

trigger EmailAfterDelete on Account(after delete) {
    Messaging.reserveSingleEmailCapacity(trigger.size);
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for (Account acct : Trigger.old) {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setToAddresses(new String[] {'some email address'};);
        email.setSubject('Deleted Account Alert');
        email.setPlainTextBody('This message is to alert you that the account named ' + acct.Name + ' has been deleted.');
        emails.add(email);
    }
    Messaging.sendEmail(emails);
}

 

All Answers

sfdcfoxsfdcfox

It would look something like this:

 

trigger NotifyOnAccountDelete on Account (after delete) {
    // Map accounts to each owner
    Map<Id, Set<Id>> notifiers = new Map<Id, Set<Id>>();
    // List of emails to deliver
    Messaging.SingleEmailMessage[] messages = new Messaging.SingleEmailMessage();
    // Initialize owner account id set
    for(Account record: Trigger.old) {
        notifiers.put(record.OwnerId, new Set<Id>());
    }
    // Populate owner account id set
    for(Account record: Trigger.old) {
        notifiers.get(record.OwnerId).add(record.Id);
    }
    // For each account owner
    for(Id ownerId: notifiers.keySet()) {
        // Make a new message
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        // Preamble
        String body = 'The following account(s) have been deleted:\r\n\r\n';
        // Include the name of each account deleted
        for(Id accountId: notifiers.get(ownerId)) {
            body += Trigger.oldMap.get(accountId).Name+'\r\n';
        }
        // Set the body to the message
        message.setPlainTextBody(body);
        // Set the subject
        message.setSubject('Account Deletion Notification');
        // Set the recipient (account owner)
        message.setTargetObjectId(ownerId);
        // Required or send will fail
        message.setSaveAsActivity(false);
        // Add to list of messages to send
        messages.add(message);
    }
    // Send the emails
    Messaging.sendEmail(messages);
}

Testing this code should be easy: Create a new account, then delete it (just a couple lines of code).

CheyneCheyne

You are right that you need to use a trigger, as workflows cannot be triggered off of deleted records. Assuming you are looking to send an email containing the account name, each time an account is deleted, you could try something like this (untested):

 

trigger EmailAfterDelete on Account(after delete) {
    Messaging.reserveSingleEmailCapacity(trigger.size);
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for (Account acct : Trigger.old) {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setToAddresses(new String[] {'some email address'};);
        email.setSubject('Deleted Account Alert');
        email.setPlainTextBody('This message is to alert you that the account named ' + acct.Name + ' has been deleted.');
        emails.add(email);
    }
    Messaging.sendEmail(emails);
}

 

This was selected as the best answer
RecondoRecondo

Thanks guys for your help! We are in business!

sfdcfoxsfdcfox

Recondo, just be aware the solution you marked will spam account owners up to 200 times more because it sends one message per record during mass deletions. Also, that solution will eat up your daily mass email limits.

RecondoRecondo

Thanks for the heads up sdfcfox!

 

We should be good to go in our instance as we have locked down the account deletion process to only sys admin at this time. We have gone through quite a bit of cleanup and only delete an account ~1/week.

 

Thanks again!

richard1.3903050990828992E12richard1.3903050990828992E12
Could anyone point me in the direction of a test class for the above trigger?
Christian Carvajal 9Christian Carvajal 9
This is very similiar as to what I need, but could this be accomplished when using an outbound message going to an external system which would need to attach an xml file?