+ Start a Discussion
sudhirn@merunetworks.comsudhirn@merunetworks.com 

Lead test class not getting coverage for trigger

Hi, 

 I wrote a trigger below which is working fine now I need to get code coverage for below trigger wrote a test class its showing only 64% coverage. what is the misssing the test class 
trigger after_territory on Lead (after insert,after update) 
{
 if(checkRecursive.runOnce())
 {
 Try
 {
 Set<Id> leadIds = new Set<Id>();
 List<Territory_Lookup__c> territoryLookupList = null;
 List<Integer> gzip = new List<Integer>(); // we need a list so that we can sort it.
 Set<String> gstate = new Set<String>();
 Set<String> gcountry = new Set<String>();

 for(Lead l :   Trigger.new){
  leadIds.add(l.id);
  gstate.add(l.state);
  gcountry.add(l.country);
   if ( l.postalcode != null)
   {
   gzip.add(Integer.valueof(l.postalcode));
   }
   if ( l.postalcode == null)
   {
   gzip.add(Integer.valueof(0));
   }
 }
 
 if ( trigger.isInsert || trigger.isupdate )
 {
   territoryLookupList =  [ select id,Area__c, Country__c, Region__c, State__c, Theater__c, User__c, Zip_End__c, Zip_Start__c
                          from Territory_Lookup__c
                          where ( Country__c = :gcountry) or
                                ( Zip_Start__c <= :gzip and Zip_End__c >=:gzip) or
                                ( Country__c = :gcountry and Zip_Start__c <= :gzip and Zip_End__c >=:gzip ) or
                                ( Country__c = :gcountry and State__c = :gstate) or
                                ( Country__c = :gcountry and Zip_Start__c <= :gzip and Zip_End__c >=:gzip and State__c = :gstate )    ];
                                
  List<Lead> leds = new List<Lead>();
  
  if ( !territoryLookupList.isEmpty() )
  {
  for(lead uld : Trigger.new){                              
   lead  uleds = new lead( Id = uld.id,Territory_Lookup__c = territoryLookupList[0].id,
                           Territory_Area__c = territoryLookupList[0].Area__c,
                           Territory_Region__c = territoryLookupList[0].Region__c,
                           Territory_Theater__c = territoryLookupList[0].Theater__c);
   leds.add(uleds);
  }
    
   }  
  else
  {
   for(lead uld : Trigger.new){                              
   lead  uleds = new lead( Id = uld.id,Territory_Lookup__c = null,
                           Territory_Area__c = null,
                           Territory_Region__c = null,
                           Territory_Theater__c = null);
   leds.add(uleds);
  
   } 
  } 
   if ( leds.size() > 0) 
    {
     update leds;
     }
     
  }
}
catch (Exception e) {
List<Lead> leds = new List<Lead>();
for(lead uld : Trigger.new){ 
 lead  uleds = new lead( Id = uld.id,
                           Territory_Lookup__c = null,
                           Territory_Area__c = null,
                           Territory_Region__c = null,
                           Territory_Theater__c = null);
   leds.add(uleds);
  }
  update leds;
}  
  }
}

Test Class
 
@isTest
private class UnitTest1 {

        static testMethod void protectFields(){

                test.startTest();
                List<Lead> lstLead =   new List<Lead>{
                          new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open',
                                   state='Karnataka',country='India',postalcode='5',LeadSource='Community')

                         };
        insert lstLead;

                lstLead[0].LastName = 'Mike';
                lstLead[0].Territory_Area__c = 'sudhir';
                lstLead[0].Territory_Region__c = 'sudhir';
                lstLead[0].Territory_Theater__c = 'sudhir';

                update lstLead;
                //CreateNewLead.CreateLead objcreateLead = new CreateNewLead.CreateLead();

                test.stopTest();
        }
}

Thanks
Sudhir
claperclaper
What lines are you not getting coverage for?
sudhirn@merunetworks.comsudhirn@merunetworks.com

Hi Claper,
 
   How to find these lines can you please tell me the step to get 
 

Thanks
Sudhir

claperclaper

On the upper right corner, click Your Name --> Developer Console --> File --> Open, search for the class, click Open. 

Then there is  button that will show you the lines that are covered in blue and the ones are not in red.

sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks am able to get the lines 

 Line 23 and 52 to 57 and Line 68 to 78 are red in color please suggest me how to fix and get 100% code coverage. 
 
trigger after_territory on Lead (after insert,after update) 
{
 if(checkRecursive.runOnce())
 {
 Try
 {
 Set<Id> leadIds = new Set<Id>();
 List<Territory_Lookup__c> territoryLookupList = null;
 List<Integer> gzip = new List<Integer>(); // we need a list so that we can sort it.
 Set<String> gstate = new Set<String>();
 Set<String> gcountry = new Set<String>();

 for(Lead l :   Trigger.new){
  leadIds.add(l.id);
  gstate.add(l.state);
  gcountry.add(l.country);
   if ( l.postalcode != null)
   {
   gzip.add(Integer.valueof(l.postalcode));
   }
   if ( l.postalcode == null)
   {
   gzip.add(Integer.valueof(0));
   }
 }
 
 if ( trigger.isInsert || trigger.isupdate )
 {
   territoryLookupList =  [ select id,Area__c, Country__c, Region__c, State__c, Theater__c, User__c, Zip_End__c, Zip_Start__c
                          from Territory_Lookup__c
                          where ( Country__c = :gcountry) or
                                ( Zip_Start__c <= :gzip and Zip_End__c >=:gzip) or
                                ( Country__c = :gcountry and Zip_Start__c <= :gzip and Zip_End__c >=:gzip ) or
                                ( Country__c = :gcountry and State__c = :gstate) or
                                ( Country__c = :gcountry and Zip_Start__c <= :gzip and Zip_End__c >=:gzip and State__c = :gstate )    ];
                                
  List<Lead> leds = new List<Lead>();
  
  if ( !territoryLookupList.isEmpty() )
  {
  for(lead uld : Trigger.new){                              
   lead  uleds = new lead( Id = uld.id,Territory_Lookup__c = territoryLookupList[0].id,
                           Territory_Area__c = territoryLookupList[0].Area__c,
                           Territory_Region__c = territoryLookupList[0].Region__c,
                           Territory_Theater__c = territoryLookupList[0].Theater__c);
   leds.add(uleds);
  }
    
   }  
  else
  {
   for(lead uld : Trigger.new){                              
   lead  uleds = new lead( Id = uld.id,Territory_Lookup__c = null,
                           Territory_Area__c = null,
                           Territory_Region__c = null,
                           Territory_Theater__c = null);
   leds.add(uleds);
  
   } 
  } 
   if ( leds.size() > 0) 
    {
     update leds;
     }
     
  }
}
catch (Exception e) {
List<Lead> leds = new List<Lead>();
for(lead uld : Trigger.new){ 
 lead  uleds = new lead( Id = uld.id,
                           Territory_Lookup__c = null,
                           Territory_Area__c = null,
                           Territory_Region__c = null,
                           Territory_Theater__c = null);
   leds.add(uleds);
  }
  update leds;
}  
  }
}

Thanks
Sudhir
claperclaper
On your test, you also need to create a couple test records for the Territory_Lookup__c object. 
sudhirn@merunetworks.comsudhirn@merunetworks.com
Hi Claper

I am doing a lookup to custom object Territory_Lookup__c passing parameter I tried to tweek bit as you suggested code coverage dropped to 63%
 
@isTest(SeeAllData = true)
private class UnitTest1 {

        static testMethod void protectFields(){
     
                test.startTest();
                List<Territory_Lookup__c> territoryLookupList = null;  
                List<Lead> lstLead =   new List<Lead>{
                          new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open',
                                   state='Karnataka',country='India',postalcode='5',LeadSource='Community')

                         };
                 insert lstLead;
                 
                territoryLookupList =  [ select id,Area__c, Country__c, Region__c, State__c, Theater__c, User__c, Zip_End__c, Zip_Start__c
                                         from Territory_Lookup__c limit 1];
                                         

                lstLead[0].LastName = 'Mike';
                lstLead[0].Territory_Area__c = territoryLookupList[0].Area__c;
                lstLead[0].Territory_Region__c = territoryLookupList[0].Region__c;
                lstLead[0].Territory_Theater__c = territoryLookupList[0].Theater__c;

                update lstLead;
                //CreateNewLead.CreateLead objcreateLead = new CreateNewLead.CreateLead();

                test.stopTest();
        }
}

 
claperclaper
instead of using the (seeAllData=true) , try actually creating the Territory_Lookup__c records on your test method as you do with the lead. Insert a couple Territory_Lookup__c records 
sudhirn@merunetworks.comsudhirn@merunetworks.com
Hi Claper, 

   I tried the same but no change code coverage is same. 

Thanks
Sudhir