• CarlR
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
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

how to make a trigger inactive in production