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
HNT_NeoHNT_Neo 

Lead Deletion Notification

Need help on creating an apex trigger. This trigger will need to notify me via email when a Lead with a certain picklist value is deleted from Salesforce from our users. Here are the core details: 

Lead Record  Type: Lead MF
Picklist API: Lead Source
Picklist Value: BODY Meters

so, if the Lead Record Type is Lead MF and the Picklist Lead Source with the picklist value BODY Meters is selected and a user deletes this lead, I want the ability to receive an email of the Lead being deleted. 

thanks!

 
Balaji BondarBalaji Bondar
Hi JH_Neo,

Please use below trigger.Update the trigger condition as per your custom field API Name:
 
trigger NotifyLeadDeletion on Lead (after delete) {
  // Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
  
  for (Lead leadobj : Trigger.old) {
    if (leadobj.RecordType == Lead MF && lead.sourcetype == 'BODY Meters') {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
      // Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(leadobj.Email);
      mail.setToAddresses(sendTo);
    
      // Step 3: Set who the email is sent from
      mail.setReplyTo('test@test.com');
      mail.setSenderDisplayName('Official Bank of Nigeria');
    
      // (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('business@test.com');
      mail.setCcAddresses(ccTo);

      // Step 4. Set email contents - you can use variables!
      mail.setSubject('Test Subject');
      String body = 'Test Body';
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
    }
  }
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(mails);
}
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
HNT_NeoHNT_Neo
Hi Balaji,

Can we shorten this? 

I, the Admin, will be the only person receiving the email notifications. 

Can you modify the Apex code to be shorter?

Thanks!
Balaji BondarBalaji Bondar
trigger NotifyLeadDeletion on Lead (after delete) {
  // Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
  
  for (Lead leadobj : Trigger.old) {
    if (leadobj.RecordType == Lead MF && lead.sourcetype == 'BODY Meters') {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
    // Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add('addyouremail@test.com');
      mail.setToAddresses(sendTo);
    
      // Step 3. Set email contents - you can use variables!
      mail.setSubject('Test Subject');
      String body = 'Test Body';
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
    }
  }
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(mails);
}
HNT_NeoHNT_Neo
trigger NotifyLeadDeletion on Lead (after delete) {
  // Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
  
  for (Lead leadobj : Trigger.old) {
    if (leadobj.RecordTypeId == '012d0000000XEVQ' && 'lead.sourcetype' == 'Axio Metrics') {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
    // Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add('nk.snhz@jfshaie.com');
      mail.setToAddresses(sendTo);
    
      // Step 3. Set email contents - you can use variables!
      mail.setSubject('AXIO Lead Deleted');
      String body = 'Need to contact user and remind not to delete AXIO Lead';
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
    }
  }
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(mails);
}
Here is what I have configured and no errors are showing up in the Devleoper Console. The Apex trigger is active, but when I created a lead with the source type and deleted the lead, I received no error message. Any ideas?
MrBrianMrBrian
I wanted to revive this thread, as it almost addresses what I am looking for. Basically, right now I getting lead deletion messages as described in this tread, however I do not want to be emailed if the lead is merged. I understand that the MasterRecordID field stores the value of the "surviving" lead on the deleted lead, but I seem to get a null value always for this field from trigger.old when the email is sent. I would like to use the criteria of the value not being null, so that I only get deletion notices on non-merging deletions. My code is as follows:
 
trigger EmailAfterDeleteLead on Lead(before delete) {
    Messaging.reserveSingleEmailCapacity(trigger.size);
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for (Lead lead : Trigger.old) {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setToAddresses(new String[] {'myname@mycompany.com'});
        email.setSubject('Deleted Lead Alert');
        email.setPlainTextBody('This message is to alert you that a lead has been deleted.' + '\n'
        + 'Lead Name: ' + lead.FirstName + ' ' + lead.LastName + '\n'
        + 'Company: ' + lead.Company + '\n'
        + 'Email: ' + lead.Email + '\n'
        + 'Id: ' + lead.Id + '\n'
        + 'Lead Owner: ' + lead.Lead_Owner_Name__c + '\n'
        + 'Current User: ' + UserInfo.getFirstName() + ' ' + UserInfo.getLastName() + '\n'
        + 'MasterRecordId: ' + lead.MasterRecordId + '\n'
        
        );
        emails.add(email);
    }
    Messaging.sendEmail(emails);
}


I have been passing the MasterRecordID value in to the email body instead of using it as criteria for now, to see if it is populating, but is alwasys returning an null value.

Please help if you can.

Thank you,
Brian
VIP Condos TorontoVIP Condos Toronto
Any luck on the deleted records notification only? (ignoring merged leads?)
HNT_NeoHNT_Neo
I'll have to test this and get back to the group, been a while! thanks!