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
Swetha A 5Swetha A 5 

Please provide me the test class for the below trigger code.

Hi all,

Below is my trigger code. But I am very poor in writing test class. So please kindly provide me the test class code for the trigger. It is urgent. Thank you in advance.
trigger UpdateActivityDetailsOnAccount on Event (after insert, after update, after delete) {

    Set <ID> acctIDs = new Set <ID> ();
    Set <ID> contactIDs = new Set <ID> ();
    Map <ID, DateTime> lastVisitDateMap = NEW Map <ID, DateTime> ();
    Map <ID, DateTime> scheduledMeetingMap = NEW Map <ID, DateTime> ();
    Map <ID, String> lastActivityTypeMap = NEW Map <ID, String> ();
    Map <ID, DateTime> lastActivityDateMap = NEW Map <ID, DateTime> ();
    
    if (!Trigger.isDelete) {
        for (Event e: Trigger.NEW) {
            if (e.WhatId != NULL) {
                if (String.valueOf(e.WhatId).startsWith('001')) {
                    acctIDs.add(e.WhatId);
                } 
            }
            if (e.WhoId != NULL) {
                if (String.valueOf(e.WhoId).startsWith('003')) {
                    contactIDs.add(e.WhoId);
                } 
            }
        }
    }
    if (Trigger.isDelete) {
        for (Event e: Trigger.OLD) {
            if (e.WhatId != NULL) {
                if (String.valueOf(e.WhatId).startsWith('001')) {
                    acctIDs.add(e.WhatId);
                } 
                if (e.WhoId != NULL) {
                    if (String.valueOf(e.WhoId).startsWith('003')) {
                        contactIDs.add(e.WhoId);
                    } 
                }
            }
        }
    }
    
    if (!contactIDs.isEmpty()) {
        List <Contact> allRelatedContacts = [ 
                                                SELECT AccountID FROM Contact 
                                                    WHERE ID IN: contactIDs
                                            ];
        for (Contact con: allRelatedContacts) {
            if (con.AccountID != NULL)
                acctIDs.add(con.AccountID);
            
        }
    }
    
    /**** To Capture Last Meeting Date ********/
    for(Account accHistory: [ SELECT 
                                (SELECT 
                                    o.What.Name , o.WhatId, o.IsTask, 
                                    o.StartDateTime, o.Subject, o.Account.Name 
                                FROM ActivityHistories o 
                                WHERE IsTask = FALSE
                                    AND Subject = 'Meeting'
                                Order By StartDateTime Desc 
                                LIMIT 1) 
                            FROM Account WHERE ID IN: acctIDs]) {
        
        if (!accHistory.ActivityHistories.isEmpty()) {
            for (ActivityHistory lastMeeting: accHistory.ActivityHistories) {
                lastVisitDateMap.put(accHistory.ID, lastMeeting.StartDateTime);
            }
        }                   
    }
    
    /**** To Capture Next Meeting Date ********/
    for(Account accHistory: [ SELECT 
                                (SELECT 
                                    o.What.Name , o.WhatId, o.IsTask, 
                                    o.StartDateTime, o.Subject, o.Account.Name 
                                FROM OpenActivities o 
                                WHERE IsTask = FALSE
                                    AND Subject = 'Meeting'
                                Order By StartDateTime ASC 
                                LIMIT 2) 
                            FROM Account WHERE ID IN: acctIDs]) {
        
        if (!accHistory.OpenActivities.isEmpty()) {
            for (OpenActivity nextMeeting: accHistory.OpenActivities) {
                scheduledMeetingMap.put(accHistory.ID, nextMeeting.StartDateTime);
            }
        }                   
    }
    
    /**** To Capture Last Activity Type & Date********/
    for(Account accHistory: [ SELECT 
                                (SELECT 
                                    o.What.Name , o.WhatId, o.IsTask, 
                                    o.StartDateTime, o.Subject, o.Account.Name 
                                FROM ActivityHistories o 
                                WHERE IsTask = FALSE
                                Order By StartDateTime Desc 
                                LIMIT 1) 
                            FROM Account WHERE ID IN: acctIDs]) {
        
        if (!accHistory.ActivityHistories.isEmpty()) {
            for (ActivityHistory lastTypeActivity: accHistory.ActivityHistories) {
                lastActivityTypeMap.put(accHistory.ID, lastTypeActivity.Subject);
                lastActivityDateMap.put(accHistory.ID, lastTypeActivity.StartDateTime);
            }
        }                   
    }
    
    List<Account> accList = NEW List<Account> ();
    for (ID accID: lastVisitDateMap.keySet()) {
        
        Account acc = NEW Account(ID = accID);
            acc.Last_Visit_Date__c = lastVisitDateMap.get(accID);
            acc.Scheduled_Visit_Date__c = scheduledMeetingMap.get(accID);
            acc.Last_Activity_Type__c = lastActivityTypeMap.get(accID);
            acc.Last_Activity_Date__c = lastActivityDateMap.get(accID);
        accList.add(acc);
        
    }
    Update accList;
    
}

 
pconpcon
Since writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started.  I would recommend that you do some reading on testing [1] [2] [3] [4] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test.

Each test should follow the following structure:
  • Setup of test data. This includes creation of any data needed by your class.  Account, Contacts etc
  • Starting the test. This is calling Test.startTest() to reset the governor limits
  • Calling your class / method
  • Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
  • Asserting that your changes have worked
    • If you have inserted/updated/deleted data, you need to query for the updates
    • Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.

[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[4] http://blog.deadlypenguin.com/blog/testing/strategies/