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
sudhirn@merunetworks.comsudhirn@merunetworks.com 

Trigger not firing for multiple conditions

Hi,

  I wrote a below trigger on lead only for condition 

if ( !gzip.isEmpty() && !gcountry.isEmpty() && !gstate.isEmpty()) it is getting fired it is not getting fired for next else if condition any thing wrong in my trigger code please suggest me. Also this is making a lookup everytime when user make a change on fields 

 
trigger territory_lookup on Lead (Before Insert, Before Update) 
{
   
 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){
  gstate.add(l.state);
  gcountry.add(l.country);
   if ( l.postalcode != null)
   {
   gzip.add(Integer.valueof(l.postalcode));
   }
 }
 

 if ( !gzip.isEmpty() && !gcountry.isEmpty() && !gstate.isEmpty()) 
{
 territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where  Zip_Start__c <= :gzip and Zip_End__c >=:gzip 
                                  limit 1]; 
} 

else if ( gzip.isEmpty() && !gcountry.isEmpty() && !gstate.isEmpty()) 
{
 territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where  State__c = :gstate and
                                  Country__c = :gcountry
                                  limit 1]; 
}

else if ( gzip.isEmpty() && !gcountry.isEmpty() && gstate.isEmpty()) 
{
 territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where  Country__c = :gcountry
                                  limit 1]; 
}

else if ( gzip.isEmpty() && gcountry.isEmpty() && !gstate.isEmpty()) 
{
 territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where  State__c = :gstate
                                  limit 1]; 
}


  for(lead uld : Trigger.new){
 
   Territory_Lookup__c tl =   getTerritoryLookup(uld);
   if(tl !=null){
     uld.Territory_Area__c = tl.Area__c;
     uld.Territory_Region__c = tl.Region__c;
     uld.Territory_Theater__c = tl.Theater__c;
    }
   }
  
  
   
 public Territory_Lookup__c getTerritoryLookup(Lead lead){
  if(territoryLookupList == null)
    return null;
  Territory_Lookup__c temp = null;
  for(Territory_Lookup__c territoryLookup : territoryLookupList){
      temp =  territoryLookup;
      break;
    
  }
  return temp;
 }
   
   
}


Thanks

Sudhir

Best Answer chosen by sudhirn@merunetworks.com
AshlekhAshlekh
Hi,

You can use debug and see the code is going.

-Thanks
Ashlekh Gera

All Answers

sudhirn@merunetworks.comsudhirn@merunetworks.com
Tried even with alternative methods its not working other than first condition
 
trigger territory_lookup on Lead (Before Insert, Before Update) 
{
   
 List<Territory_Lookup__c> territoryLookupList = null;  
 List<Integer> gzip = new List<Integer>(); // we need a list so that we can sort it.
 list<String> gstate = new list<String>();
 list<String> gcountry = new list<String>(); 
 
 for(Lead l :   Trigger.new){
  gstate.add(l.state);
  gcountry.add(l.country);
   if ( l.postalcode != null)
   {
   gzip.add(Integer.valueof(l.postalcode));
   }
 }
 

 if ( gzip.size() > 0 ) 
{
 territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where  Zip_Start__c <= :gzip and Zip_End__c >=:gzip 
                                  limit 1]; 
} 
 else if ( gstate.size() > 0 && gcountry.size() > 0 )
{
 territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where  State__c = :gstate and 
                                  Country__c = :gcountry
                                  limit 1]; 
}
 else if ( gcountry.size() > 0 )
{
 territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where  Country__c = :gcountry
                                  limit 1]; 
}

  for(lead uld : Trigger.new){
 
   Territory_Lookup__c tl =   getTerritoryLookup(uld);
   if(tl !=null){
     uld.Territory_Area__c = tl.Area__c;
     uld.Territory_Region__c = tl.Region__c;
     uld.Territory_Theater__c = tl.Theater__c;
    }
   }
  
  
   
 public Territory_Lookup__c getTerritoryLookup(Lead lead){
  if(territoryLookupList == null)
    return null;
  Territory_Lookup__c temp = null;
  for(Territory_Lookup__c territoryLookup : territoryLookupList){
      temp =  territoryLookup;
      break;
    
  }
  return temp;
 }
   
   
}

Thanks
Sudhir
AshlekhAshlekh
Hi,

You can use debug and see the code is going.

-Thanks
Ashlekh Gera
This was selected as the best answer
CarlRCarlR
Since your IF statements are mutually exclusive, why not simply have 3 different IF statements rather than using the } else if { construct.
sudhirn@merunetworks.comsudhirn@merunetworks.com
Hi CarIR,

 I even tried alternative way first condition works perfect second is not working I am doing a looking from the method inside the trigger is there any alternative suggestion to tweek the code please suggest me
 
trigger territory_lookup on Lead (Before Insert, Before Update) 
{
   
 List<Territory_Lookup__c> territoryLookupList = null;  
 List<Integer> gzip = new List<Integer>(); // we need a list so that we can sort it.
 list<String> gstate = new list<String>();
 list<String> gcountry = new list<String>(); 
 
 for(Lead l :   Trigger.new){
  gstate.add(l.state);
  gcountry.add(l.country);
   if ( l.postalcode != null)
   {
   gzip.add(Integer.valueof(l.postalcode));
   }
 }
 

 if ( gzip.size() > 0 ) 
{
 territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where  Zip_Start__c <= :gzip and Zip_End__c >=:gzip 
                                  limit 1]; 
} 

if ( gcountry.size() > 0 )
{
 territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where  Country__c in :gcountry
                                  limit 1]; 
}

  for(lead uld : Trigger.new){
 
   Territory_Lookup__c tl =   getTerritoryLookup(uld);
   if(tl !=null){
     uld.Territory_Lookup__c = tl.id;
     uld.Territory_Area__c = tl.Area__c;
     uld.Territory_Region__c = tl.Region__c;
     uld.Territory_Theater__c = tl.Theater__c;
    }
   }
  
  
   
 public Territory_Lookup__c getTerritoryLookup(Lead lead){
  if(territoryLookupList == null)
   return null;
  Territory_Lookup__c temp = null;
  for(Territory_Lookup__c territoryLookup : territoryLookupList){
      
      temp =  territoryLookup;
      break;
      
    
  }
  return temp;
 }
   
   
}


Thanks
Sudhir