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
Che SFDCChe SFDC 

Help with test class for Event email trigger

Hello, I`m new to Apex programming and I need help with test class. Below is my trigger and i`m not getting more than 54% test coverage on this trigger.  Can someone please help me with test code? Thanks in advance.
 
Trigger VisitNotification on Event (after insert){
		
    List<Messaging.SingleEmailMessage> newEmails = new List<Messaging.SingleEmailMessage>();
	List<Id> whatIds = new List<Id>();
	List<Id> createdByIds = new List<Id>();
	for(Event e :Trigger.new)
	{
		createdByIds.add(E.CreatedByID);
		whatIds.add(E.whatId);
	}
	Map<Id, User> users = new Map<Id, User>([SELECT Id, Name from User WHERE Id in :createdByIds]);
	Map<Id, Opportunity> opportunities = new Map<Id, Opportunity>([SELECT Name, Account.Name from Opportunity WHERE Id in :whatIds]);
    for(Event e :Trigger.new){
            String fullTaskURL;
            String Facility;
            Datetime startDate;
            Datetime endDate;
            String Purpose;
            String Salesexec;

            // Query to populate CreatedBy name in html template
            Id createdByID = E.CreatedByID;
            String createdByName = users.get(createdByID).Name;
            
            // Query to populate Opportunity name in html template
            String oppID = E.whatId;  
            String oppName = opportunities.get(oppID).Name;
    
        if(E.Visit__C){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                
            mail.setSubject('Automated Alert: A New Record is Created');
            mail.setSaveAsActivity(false);
            mail.setTargetObjectId('00580000005HreR') ;        

            fullTaskURL = URL.getSalesforceBaseUrl().toExternalForm() + '/' + E.Id;
            Facility = E.Facility__c;
            startDate = E.StartDateTime;
            endDate = E.EndDateTime;
            Purpose = E.Purpose__c;
            Salesexec = E.Salesexec__c;
   
   
            //Generate email template
                String emailBody;
                emailBody = '<html><body>Hi<br> <br> A New Record has been created: <a href="' + fullTaskURL + '">' + E.Id + '</a><br></body></html>' + '<br> <br> Site/Facility: ' + Facility+ '<br> Start Date/Time: ' + StartDate + '<br> End Date/Time: ' +endDate + '<br> Purpose: ' + Purpose + '<br> Sales VP: '+ Salesexec +  '<br> <br> Thank you <br> <br> Salesforce Admin' ; 
                mail.setHtmlBody(emailBody );

            newEmails.add(mail);
        }
    }

    messaging.sendEmail(newEmails);
}

 
Best Answer chosen by Che SFDC
pconpcon
Can you please also include the tests that you have written?  Without knowing what you have done to test it, my guess would be that you have not written mutliple tests that contain different data that follow all of the paths of your code.

For this code here you should have at least 5 tests
  • One that inserts a single Event where visit__c is false and where whatId is set to a valid opportunity
  • One that inserts a single Event where visit__c is false and where whatId is not an opportunity
  • One that inserts a single Event where visit__c is true and where whatId is set to a valid opportunity
  • One that inserts a single Event where visit__c is true and where whatId is not an opportunity
  • One that inserts several Events with a mixture of visit__c and whatId
Based on your current code, you would get a Null Pointer Exception if the whatId of the Event is not an opportunity, since the Map of Opportunitites will return null and you are then trying to pull Name off of it.

All Answers

pconpcon
Can you please also include the tests that you have written?  Without knowing what you have done to test it, my guess would be that you have not written mutliple tests that contain different data that follow all of the paths of your code.

For this code here you should have at least 5 tests
  • One that inserts a single Event where visit__c is false and where whatId is set to a valid opportunity
  • One that inserts a single Event where visit__c is false and where whatId is not an opportunity
  • One that inserts a single Event where visit__c is true and where whatId is set to a valid opportunity
  • One that inserts a single Event where visit__c is true and where whatId is not an opportunity
  • One that inserts several Events with a mixture of visit__c and whatId
Based on your current code, you would get a Null Pointer Exception if the whatId of the Event is not an opportunity, since the Map of Opportunitites will return null and you are then trying to pull Name off of it.
This was selected as the best answer
Che SFDCChe SFDC
Thanks pcon ... this helps. I now get 100% test coverage on my trigger.