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 a test class for this lead trigger?

It was previously working correctly at 80% coverage. I've since made changes to the trigger and now its at 66%. The changes are the addition of a US_zip_code__c checkbox. Previously the trigger could only assign to UserAssigned or User2 and now we can also assign to Mex CB RSM or Mex WH RSM. I commented out the continue line as marked in the code and added the two new user fields (Mex CB RSM and Mex WH RSM) to the query in order to grab this information to use.
Anybody good at writing test classes that can help?

Trigger:
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)
    {       
         if (l.Do_Not_Apply_Zip_Assignment_Rules__c == false && (l.LeadSource == 'WPP-2020' || l.LeadSource == '346 Leasing - 2020' || l.LeadSource == '1219 Leasing - 2020' || l.LeadSource == 'Hydro Leasing - 2020' || l.LeadSource == 'Chatbot')){
    
        System.debug('LeadAssignmentByZipCode -- Do_Not_Use_ZipAssignment_del__c ' + l.Do_Not_Apply_Zip_Assignment_Rules__c);
        
      
        if(Trigger.isUpdate){
            System.debug('LeadAssignmentByZipCode -- Update Trigger -- ');
            Lead oldLead = Trigger.oldMap.get(l.id);
            //CHANGE****NOW COMMENTED OUT***: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, User2__c, Mex_WH_RSM__c, Mex_CB_RSM__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 == false && (lead.LeadSource == 'WPP-2020' || lead.LeadSource == '346 Leasing - 2020' || lead.LeadSource == '1219 Leasing - 2020' || lead.LeadSource == 'Hydro Leasing - 2020' || lead.LeadSource == 'Chatbot')){
        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 -- ');
        
        //CHANGES****Added logic to handle a US_Zip_Code__c field and two new users that can be assigned as owner***
        if(zipCodeMap.containsKey(lead.PostalCode)){
        if(lead.US_Zip_Code__c == true){
        
          //  Id owner = zipCodeMap.get(lead.PostalCode).UserAssigned__c;
          //  lead.OwnerId = owner;
            
           Id owner;
           Zip_Code__c zipCode = zipCodeMap.get(lead.PostalCode);

          if(lead.LeadSource == 'WPP-2020' || lead.LeadSource == '346 Leasing - 2020'){
              if(zipCode.user2__c != null) {
                owner = zipCode.User2__c;
                }
              else {
                owner = zipCode.UserAssigned__c;
                } 
          }
         
          else {
              if(zipCode.UserAssigned__c != null) {
                owner = zipCode.UserAssigned__c;
               }
        
              else {
               owner = zipCode.User2__c;
              }
              }
          lead.OwnerId = owner;
         
            
            lead.Dealer_Lead_Person__c = zipCodeMap.get(lead.PostalCode).Dealer_Lead_Email_Address__c;
            
            }
            
            else{
        
        
          //  Id owner = zipCodeMap.get(lead.PostalCode).UserAssigned__c;
          //  lead.OwnerId = owner;
            
           Id owner;
           Zip_Code__c zipCode = zipCodeMap.get(lead.PostalCode);

          if(lead.LeadSource == 'WPP-2020' || lead.LeadSource == '346 Leasing - 2020'){
              if(zipCode.Mex_WH_RSM__c != null) {
                owner = zipCode.Mex_WH_RSM__c;
                }
              else {
                owner = zipCode.Mex_CB_RSM__c;
                } 
          }
         
          else {
              if(zipCode.UserAssigned__c != null) {
                owner = zipCode.Mex_CB_RSM__c;
               }
        
              else {
               owner = zipCode.Mex_WH_RSM__c;
              }
              }
          lead.OwnerId = owner;
         
            
            lead.Dealer_Lead_Person__c = zipCodeMap.get(lead.PostalCode).Dealer_Lead_Email_Address__c;
            
            }
            }
            }
           }}

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';
            lead.LeadSource = 'WPP-2020';
            insert lead;
            
            lead.PostalCode = '20710';
            
            
            update lead;
            
            String s2 = CheckOrganizationID.getServer();
                        
           
            
            test.stopTest();
            
            
            
            
        }catch (Exception e){
        
            System.Debug('Error While Testing testLeadAssignedByZipCode');
            
        }           

    }
}

 
Best Answer chosen by Jason Adler 9
Jason Adler 9Jason Adler 9
Hi, thank you for your answer. That did not work though. Still at 66% with that addition.

I managed to get to 88% coverage by adding more methods. Seems lengthy and probably an easier solution, but the coverage is good enough. Here is the code that is working in case you were wondering:
 
@isTest
public 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';
            lead.LeadSource = 'Hydro Leasing - 2020';
            insert lead;
            
            lead.PostalCode = '20710';
            
            
            update lead;
            
            String s2 = CheckOrganizationID.getServer();
                  
            
            test.stopTest();
            
            
            
            
        }catch (Exception e){
        
            System.Debug('Error While Testing testLeadAssignedByZipCode');
            
        }           

    }
    static testMethod void testLeadAssignedByZipCode2() {

        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';
            lead.LeadSource = 'Chatbot';
            lead.US_Zip_Code__c = false;
            insert lead;
            
            lead.PostalCode = '20710';
            
            
            update lead;
            
            
            String s2 = CheckOrganizationID.getServer();
                  
            
            test.stopTest();
            
            
            
            
        }catch (Exception e){
        
            System.Debug('Error While Testing testLeadAssignedByZipCode');
            
        }           

    }
    
    static testMethod void testLeadAssignedByZipCode3() {

        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';
            lead.LeadSource = 'WPP-2020';
            lead.US_Zip_Code__c = false;
            insert lead;
            
            lead.PostalCode = '20710';
            
            
            update lead;
            
            
            String s2 = CheckOrganizationID.getServer();
                  
            
            test.stopTest();
            
            
            
            
        }catch (Exception e){
        
            System.Debug('Error While Testing testLeadAssignedByZipCode');
            
        }           

    }
    static testMethod void testLeadAssignedByZipCode4() {

        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';
            lead.LeadSource = 'WPP-2020';
            
            insert lead;
            
            lead.PostalCode = '20710';
            
            
            update lead;
            
            
            String s2 = CheckOrganizationID.getServer();
                  
            
            test.stopTest();
            
            
            
            
        }catch (Exception e){
        
            System.Debug('Error While Testing testLeadAssignedByZipCode');
            
        }           

    }
}

 

All Answers

PriyaPriya (Salesforce Developers) 

Hi Jason,

Please use the below test class code to increase the code coverage :- 
 

@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';
            lead.LeadSource = 'WPP-2020';
            insert lead;
            
            lead.PostalCode = '20710';
            
            
            update lead;
            
            lead.US_zip_code__c = True;
            
            update lead;
            
            String s2 = CheckOrganizationID.getServer();
                        
           
            
            test.stopTest();
            
            
            
            
        }catch (Exception e){
        
            System.Debug('Error While Testing testLeadAssignedByZipCode');
            
        }           

    }
}
 


Please mark it as best answer if it helps you to fix the issue.

Thank you!

 

Jason Adler 9Jason Adler 9
Hi, thank you for your answer. That did not work though. Still at 66% with that addition.

I managed to get to 88% coverage by adding more methods. Seems lengthy and probably an easier solution, but the coverage is good enough. Here is the code that is working in case you were wondering:
 
@isTest
public 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';
            lead.LeadSource = 'Hydro Leasing - 2020';
            insert lead;
            
            lead.PostalCode = '20710';
            
            
            update lead;
            
            String s2 = CheckOrganizationID.getServer();
                  
            
            test.stopTest();
            
            
            
            
        }catch (Exception e){
        
            System.Debug('Error While Testing testLeadAssignedByZipCode');
            
        }           

    }
    static testMethod void testLeadAssignedByZipCode2() {

        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';
            lead.LeadSource = 'Chatbot';
            lead.US_Zip_Code__c = false;
            insert lead;
            
            lead.PostalCode = '20710';
            
            
            update lead;
            
            
            String s2 = CheckOrganizationID.getServer();
                  
            
            test.stopTest();
            
            
            
            
        }catch (Exception e){
        
            System.Debug('Error While Testing testLeadAssignedByZipCode');
            
        }           

    }
    
    static testMethod void testLeadAssignedByZipCode3() {

        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';
            lead.LeadSource = 'WPP-2020';
            lead.US_Zip_Code__c = false;
            insert lead;
            
            lead.PostalCode = '20710';
            
            
            update lead;
            
            
            String s2 = CheckOrganizationID.getServer();
                  
            
            test.stopTest();
            
            
            
            
        }catch (Exception e){
        
            System.Debug('Error While Testing testLeadAssignedByZipCode');
            
        }           

    }
    static testMethod void testLeadAssignedByZipCode4() {

        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';
            lead.LeadSource = 'WPP-2020';
            
            insert lead;
            
            lead.PostalCode = '20710';
            
            
            update lead;
            
            
            String s2 = CheckOrganizationID.getServer();
                  
            
            test.stopTest();
            
            
            
            
        }catch (Exception e){
        
            System.Debug('Error While Testing testLeadAssignedByZipCode');
            
        }           

    }
}

 
This was selected as the best answer