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
Chad CoffeyChad Coffey 

Help with Apex Test Class

I have the below trigger built to notify user(s) when a contact is deleted, however, I am struggling with the test class for this trigger.  Any help would be greatly appreciated. 

trigger EmailAfterDelete on Contact (after delete) {
  Messaging.reserveSingleEmailCapacity(trigger.size);
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for (Contact acct : Trigger.old) {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setToAddresses(new String[] {'ccoffey@brooksource.com'});
        email.setSubject('Deleted Contact Alert');
        email.setPlainTextBody('This message is to alert you that a Contact has been deleted.');
        emails.add(email);
    }
    Messaging.sendEmail(emails);
}
Best Answer chosen by Chad Coffey
Abdul KhatriAbdul Khatri
@isTest
public class EmailAfterDelete_Test {

   public static testMethod void test_email_sent_contact_detele() {
   
       Contact contact = new Contact (FirstName = 'Abdul', LastName = 'Khatri');
       insert contact;
       
       Test.startTest();
			Database.delete(contact);
       Test.stopTest();
   }
}

 

All Answers

Abdul KhatriAbdul Khatri
@isTest
public class EmailAfterDelete_Test {

   public static testMethod void test_email_sent_contact_detele() {
   
       Contact contact = new Contact (FirstName = 'Abdul', LastName = 'Khatri');
       insert contact;
       
       Test.startTest();
			Database.delete(contact);
       Test.stopTest();
   }
}

 
This was selected as the best answer
Abdul KhatriAbdul Khatri
Hey Was this helpful?
Chad CoffeyChad Coffey
Yes, Abhul that worked perfectly!  Thank you very much!!