You need to sign in to do that
Don't have an account?
Jason Adler 9
And here is the current test class:
I'm stuck at 51%, can anyone help?
Can anyone help with this test class, I'm at 51% coverage?
trigger LeadAssignOwnerByPostalCode on Lead (before insert, before update) { List<Lead> lList = new List<Lead>(); Map<String,Zip_Code__c> zipCodeMap = new Map<String,Zip_Code__c>(); Map<id, User> userMap = new Map<id, User>(); Set<String> userIds = new Set<String>(); Set<String> zipCodeString = new Set<String>(); List<Zip_Code__c> zipCodeList = new List<Zip_Code__c>(); List<Messaging.SingleEmailMessage> listmail = new List<Messaging.SingleEmailMessage>(); for(Lead l: Trigger.new) { System.debug('LeadAssignmentByZipCode -- Do_Not_Use_ZipAssignment_del__c ' + l.Do_Not_Apply_Zip_Assignment_Rules__c); //Do_Not_Use_ZipAssignment_del__c if(l.Do_Not_Apply_Zip_Assignment_Rules__c == true || l.LeadSource != 'Lead from Hubspot') continue; if(Trigger.isUpdate){ System.debug('LeadAssignmentByZipCode -- Update Trigger -- '); Lead oldLead = Trigger.oldMap.get(l.id); if(l.PostalCode.equals(oldLead.PostalCode)) continue; } else System.debug('LeadAssignmentByZipCode -- Insert Trigger -- '); if(l.PostalCode <> null){ zipCodeString.add(l.PostalCode); System.debug('LeadAssignmentByZipCode -- Lead -- ' + l.Name + ' Zip -- ' + l.PostalCode); } } if(!zipCodeString.IsEmpty()) { System.debug('LeadAssignmentByZipCode -- Leads To Process ' + zipCodeString.size()); zipCodeList = [SELECT id, Name, UserAssigned__c, Dealer_Lead_Email_Address__c FROM Zip_Code__c WHERE Name IN: zipCodeString ]; } else{ System.debug('LeadAssignmentByZipCode -- No Leads To Process'); } if(!zipCodeString.IsEmpty()){ System.debug('LeadAssignByZip --- zipCodeString Count --> ' + zipCodeString.size()); for(Zip_Code__c z: zipCodeList){ if(!zipCodeMap.containsKey(z.Name)){ zipCodeMap.put(z.name, z); userIds.add(z.UserAssigned__c); } } }else System.debug('LeadAssignByZip --- zipCodeString Count --> zero'); if(!userIds.IsEmpty()){ System.debug('LeadAssignByZip --- userIds Count --> ' + userIds.size()); userMap = new Map<id,User>([SELECT id, email FROM User WHERE id IN: userIDs]); }else System.debug('LeadAssignByZip --- userIds Count --> zero'); for(Lead lead: Trigger.New) { if(lead.Do_Not_Apply_Zip_Assignment_Rules__c == true) continue; if(Trigger.isUpdate){ System.debug('LeadAssignmentByZipCode -- Update Trigger -- '); Lead oldLead = Trigger.oldMap.get(lead.id); if(lead.PostalCode.equals(oldLead.PostalCode)) continue; } else System.debug('LeadAssignmentByZipCode -- Insert Trigger -- '); if(zipCodeMap.containsKey(lead.PostalCode)){ Id owner = zipCodeMap.get(lead.PostalCode).UserAssigned__c; lead.OwnerId = owner; lead.Dealer_Lead_Person__c = zipCodeMap.get(lead.PostalCode).Dealer_Lead_Email_Address__c; //Added by Abhishek : End } } }
And here is the current test class:
@isTest(SeeAllData=true) private class TestLeadAssignmentByZipCode { static testMethod void testLeadAssignedByZipCode() { try{ test.startTest(); Zip_Code__c zip = new ZIP_Code__c(); zip.Name = '20716'; zip.UserAssigned__c = UserInfo.getUserId(); insert zip; Lead lead = new Lead(); lead.FirstName = 'James'; lead.Company = 'Acme'; lead.Street = '100 West Street'; lead.City = 'Marlboro'; lead.State = 'MD'; lead.PostalCode = '20716'; insert lead; lead.PostalCode = '20710'; update lead; String s2 = CheckOrganizationID.getServer(); test.stopTest(); }catch (Exception e){ System.Debug('Error While Testing testLeadAssignedByZipCode'); } } }
I'm stuck at 51%, can anyone help?
Since you have not set the LeadSource, the code is being skipped. Which means the rest of the IF statements will fail as you have never built the first Set of Strings zipCodeString
add to your test data.
As a side note, I would adjust your variable names so that they more accurately match their content - zipCodeString makes me think of an individual String, not a set of Strings. But zipCodeStringSet or setZipCodes makes me think of a collection.
regards
Andrew
All Answers
Few seggestions:
1. Move trigger code to the helper class
2. Use some trigger framework (for e.g. https://www.biswajeetsamal.com/blog/salesforce-apex-trigger-framework/)
3. Add comments to the code
4. Don't use SeeAllData=true
5. User test data factory class
Since you have not set the LeadSource, the code is being skipped. Which means the rest of the IF statements will fail as you have never built the first Set of Strings zipCodeString
add to your test data.
As a side note, I would adjust your variable names so that they more accurately match their content - zipCodeString makes me think of an individual String, not a set of Strings. But zipCodeStringSet or setZipCodes makes me think of a collection.
regards
Andrew