+ Start a Discussion
Mehul pMehul p 

Need help with Test class for Contact Trigger

Hi everyone,

     I have this trigger on contact and I have no idea how to write the test class for this. Any help will be greatly appreciated. 

Thank you in advance!
public class contactHandlerClass {
     public static boolean contactRecursion =false; // to avoid the "maximum trigger depth exceeded" error


    public static void getRecentTaskInformation(List<Contact> contacts) {
       
        System.debug('print the list' + contacts);
 //Tasks     
        List<Contact> contactsWithLastTasks = [
            SELECT Id, Last_Activity_Subject__c, Last_Activity_Date__c,Contact_Task_Date__c,	Most_Recent_Contact_Task_Subject__c,
                Last_Activity_Name__c , Last_Activity_Assigned_to__c ,
                Last_Activity_TypeOfInteraction__c , Name, (
                    SELECT Subject, ActivityDate, Owner.Name,Task.WhoId,
                        Type_of_Interaction__c
                    FROM Tasks
                    ORDER BY ActivityDate DESC 
                    LIMIT 1
                )
            FROM Contact WHERE Id IN :contacts
        ];
      
        for (Contact c : contactsWithLastTasks) {
            if (!c.tasks.isEmpty()) {
                Task lastTask = c.tasks[0];
                System.debug('print Tasks' + lastTask);

                c.Most_Recent_Contact_Task_Subject__c = lastTask.Subject;
                c.Contact_Task_Date__c = lastTask.ActivityDate;
                // may be create field and set it text "Task" to identify if it is Task or Event
                
                System.debug('Print contacts list one by one?'+ contactsWithLastTasks);
                System.debug('Field one?'+ c.Most_Recent_Contact_Task_Subject__c);
                System.debug('Field two?'+ c.Contact_Task_Date__c);
        
            }
        }
        If(!contactsWithLastTasks.isEmpty()){
        Update contactsWithLastTasks;
        }
 //Events
        List<Contact> contactsWithLastEvents = [
            SELECT Id, Last_Activity_Subject__c, Last_Activity_Date__c,Contact_Task_Date__c,	Most_Recent_Contact_Task_Subject__c,
                Last_Activity_Name__c , Last_Activity_Assigned_to__c ,
                Last_Activity_TypeOfInteraction__c , Name, (
                    SELECT Subject, StartDateTime
                    FROM Events
                    ORDER BY ActivityDate DESC 
                    LIMIT 1
                )
            FROM Contact WHERE Id IN :contacts
        ];
      
        for (Contact c : contactsWithLastEvents) {
            if (!c.events.isEmpty()) {
                Event lastEvent = c.events[0];
                System.debug('print Tasks' + lastEvent);

                c.Most_Recent_Contact_Task_Subject__c = lastEvent.Subject;
                //c.Contact_Task_Date__c = lastEvent.;
                // may be create field and set it text "Task" to identify if it is Task or Event
                
                System.debug('Print contacts list one by one?'+ contactsWithLastEvents);
                System.debug('Field one?'+ c.Most_Recent_Contact_Task_Subject__c);
                System.debug('Field two?'+ c.Contact_Task_Date__c);
        
            }
        }
        If(!contactsWithLastEvents.isEmpty()){
        Update contactsWithLastEvents;
        }
    }

}


 
Jeff Hutchinson 17Jeff Hutchinson 17
Basically, you need to create a test class using the @isTest notation. Within that class create one or more contacts and insert and/or update them to make your trigger fire. Afterward, make an assertion to ensure your trigger operated correctly. You can find a detailed explaination at
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_triggers