You need to sign in to do that
Don't have an account?
Need help with Test Class Coverage: how to get to 100%
Hi all... here's my first trigger. I'm setting three field values on a lead (a four hour follow up time in business hours; an eight hour follow up time in business hours; and grabbing the users manager's email to store on the lead for emailing purposes). I've got it all working and I've got a test class working. Everything is fine except I can only get my test class coverage to 91%. How do I get to 100%? It was at 100% until I enhanced my trigger code to work better. Please help!
Trigger code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | trigger calcLeadFollowUpTimes on Lead (before insert) { BusinessHours stdBusinessHours = [select id from businesshours where Name = 'JELD-WEN Leads']; String mgrEmail = 'email@email.com'; String mgrID = ''; for (Lead LD: Trigger.new) { mgrID = [select ManagerId from user where user.Id =: LD.ownerId].ManagerId; if ( mgrID != NULL) { mgrEmail = [select Email from user where user.id =: mgrID].email; } if ( stdBusinessHours != NULL) { // We currently have 12 hours / day (5:00 AM - 5:00 PM, M-F). Our SLA is 8 business hours. This trigger calculates 4 business hours as a halfway point reminder. LD.Four_Hour_Follow_Up_Time__c = BusinessHours.addGmt (stdBusinessHours.id, System.Now(), 4 * 60 * 60 * 1000L); LD.Eight_Hour_Follow_Up_Time__c = BusinessHours.addGmt (stdBusinessHours.id, System.Now(), 8 * 60 * 60 * 1000L); } if ( mgrEmail != NULL) { //Sets the manager's email field so that manager can be notified LD.LeadOwnersManagersEmail__c = mgrEmail; } } } |
Now here is my test class code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | @isTest private class testLeadFollowUpsClass { static testMethod void insertLead() { // Insert a lead System.debug('NJ:Inserting Lead to test follow up times'); Lead ldDated = new Lead (LastName='TestLead123123', Company='JW', Status='New', Lead_Type__c='Architect'); insert ldDated; // Read the record back and verify that the fields modified by this trigger are not null ldDated = [select Id, LastName, Four_Hour_Follow_Up_Time__c, Eight_Hour_Follow_Up_Time__c, LeadOwnersManagersEmail__c from Lead where LastName = 'TestLead123123']; System.assert (ldDated.Four_Hour_Follow_Up_Time__c != NULL); System.assert (ldDated.Eight_Hour_Follow_Up_Time__c != NULL); System.assert (ldDated.LeadOwnersManagersEmail__c != NULL); System.assert (ldDated.Id !=NULL); } } // testLeadFollowUpsClass |
Any advice? I'm brand new to APEX and fairly new to programming anything in general so please explain. Thanks, Neil
EDIT:
One other thing I would be interested in knowing is if there is anything I can do to make sure that this trigger doesn't prevent a user from saving a lead. In other words is there an exception handling?
Thanks!
For adding exceptions in Trigger code you can use addError, It will prevent any DML
click here for documentation
Thanks,
Bala
Bala, I've read through the documentation and don't quite understand. My goal is that if the trigger fails it won't fail the insert of the record. If I add this to my code it will allow the trigger to fail without failing the record insert?
Also how do you make it so your code shows up in a box? I've been trying to figure that out.
Thanks,