You need to sign in to do that
Don't have an account?

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); }
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
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.