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
Jason Adler 9Jason Adler 9 

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?
Best Answer chosen by Jason Adler 9
Andrew GAndrew G
I would hazard a guess that this line is your issue:
if(l.Do_Not_Apply_Zip_Assignment_Rules__c == true || l.LeadSource != 'Lead from Hubspot') continue;
I will guess that Do_Not_Apply_Zip_Assignment_Rules__c  will be false by default, but your LeadSource  is not being set in your test 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.
lead.LeadSource = "Lead from Hubspot"

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

RituSharmaRituSharma
Highlight the lines of code that are not getting covered. That would help to find out the scenarios that your test code is not covering as of now. 

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
Jason Adler 9Jason Adler 9
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;
          
            }
            }

}

 
Andrew GAndrew G
I would hazard a guess that this line is your issue:
if(l.Do_Not_Apply_Zip_Assignment_Rules__c == true || l.LeadSource != 'Lead from Hubspot') continue;
I will guess that Do_Not_Apply_Zip_Assignment_Rules__c  will be false by default, but your LeadSource  is not being set in your test 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.
lead.LeadSource = "Lead from Hubspot"

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


 
This was selected as the best answer
Jason Adler 9Jason Adler 9
Thank you so much, that line did the trick!