Triggers – Send email after record deletion.

There are occasions in which you want to notify a person or a group of people when a record/s has been deleted.
To accomplish this you can write a trigger that will send an email to those that need to be informed about this action.

We will write a trigger here that will do exactly that, it will send an email to one email address if a record gets deleted. We are not going to write the full logic in the trigger’s body. We are going to build a helper class to take care of the execution of our logic and we are going to invoke this helper class from all class.

So let’s start by writing out ONE AND ONLY trigger for all Opportunity’s context variables.

*    Trigger for the Opportunity object. All trigger context variables. 
trigger TriggerOnOpportunity on Opportunity(before delete, before insert, before update, 
                                after delete, after insert, after update) {

 if(Trigger.isBefore && Trigger.isDelete)

On that code, you can see you added all context variables, so all before and after DML options are there.
Later on the code, we are implicitly asking the trigger to invoke a class ‘BeforeDeletingOpportunities‘ if the Trigger.isBefore and Trigger.isDelete. Our class, that we will be taking a look later, will receive our opportunities and it will pass them to a method,“beforeDeleteOperations()”. This kind of structure will help you to have control over your code. In this example, we could also add a new class doing some other operations and decide if we want those actions to take place before or after or class ‘BeforeDeletingOpportunities’.

Why will you want to do something like this? The simple answer to that will be that you could have different business requirements asking you to do something else before sending that email on the same trigger context.

Now we take a look to our helper class hat will contain the logic for before deleting an Opportunity.
Notice that we are using the ‘with sharing’ access modifier when declaring our class.
This will only allow the user running this class to perform this operation reinforcing SFDC access levels.

*    Helper class for Opportunity deleting processes

public with sharing class BeforeDeletingOpportunities

  public static void beforeDeleteOperations()
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

  for(Opportunity opp: (List<Opportunity>) trigger.old)
     Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
     email.setToAddresses(new String[] {''});
     email.setSubject('Deleted Opportunity Alert');
     email.setPlainTextBody('This message is to alert you that the Opportunity named ' + opp.Name + ' has been deleted.');



If you want to send this notification to more than one person, all you need to do is to add their email address on this line of the code:

 email.setToAddresses(new String[] {'',''});


Leave a Reply to Shubhada Jadhav Cancel reply

Your email address will not be published. Required fields are marked *