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 is not updating values

Hi,
 
 Wrote a below trigger after insert and after update for some reasons its not firing please suggest me what might be the issue. 
 
trigger territory_lookup on Lead (After Insert, After Update) 
{
  Try 
  {
 set<id> gid = new set<id>();
 Set<String> gzip = new Set<String>();
 Set<String> gstate = new Set<String>();
 Set<String> gcountry = new Set<String>();
 
 Integer intzip;

 lead ld = [select id,postalcode,state,country from lead where id = :Trigger.newMap.keySet() limit 1];

 intzip=Integer.valueof(ld.postalcode);

  system.debug(intzip);
 
 String ldstate;
 String ldcountry;
 
  ldstate = ld.state;
  ldcountry = ld.country;
  Territory_Lookup__c tl = [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where Zip_Start__c <= :intzip and Zip_End__c >= :intzip and
                                 State__c in :gstate and Country__c in :gcountry limit 1];
                                

  list<lead> led = [select id,postalcode,state,country from lead where id = :Trigger.newMap.keySet() limit 1];
 
  for(lead uld : led)
  {
   uld.Territory_Area__c = tl.Area__c;
   uld.Territory_Region__c = tl.Region__c;
   uld.Territory_Theater__c = tl.Theater__c;
   }

  update led;

 }  
     
  catch(Exception ex){
  system.debug(ex);

  
  }    

}

Thanks
Sudhir
Best Answer chosen by sudhirn@merunetworks.com
reymagdaongreymagdaong
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>(); 

 Integer zipLowerBound = 0;
 Integer zipUpperBound = 0;
 
 for(Lead l : (Lead) Trigger.new){
  gstate.add(l.state);
  gcountry.add(l.country);
  gzip.add(Integer.valueof(l.postalcode));
 }

 gzip.sort(); //sort the list from lowest to highest
 zipLowerBound = gzip.get(0); //get the lowest
 zipUpperBound = gzip.get(gzip.size() - 1); //get the highes value
 
 territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where Zip_Start__c >= :zipLowerBound and Zip_End__c <=:zipUpperBound and
                                 State__c in :gstate and Country__c in :gcountry];
  
  for(lead uld : (Lead) 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){
    Integer zip = Integer.valueof(lead.postalcode);
    if(territoryLookup.State__c == lead.state &&
      territoryLookup.Country__c == lead.country &&
      territoryLookup.Zip_Start__c <= zip&&
      territoryLookup.Zip_End__c >= zip ){
      temp =  territoryLookup;
      break;
    }
  }
  return temp;
 }
}
hi, i tried to refactor your code so that it is bulkified. see if this is correct, haven't tried to compile this so you may find some compile error which you should be able to resolve.

All Answers

reymagdaongreymagdaong
Try to add this after line 34, led.add(uld).

On the other note, i suggest to do this before insert/update.

and why limit to 1?
sudhirn@merunetworks.comsudhirn@merunetworks.com
Try no luck 

My reqyirement is as user change the postalcode in lead trigger my fire and update the values  below code is working fine in developer console but it is not working when applied in trigger after insert and after update please suggest me how to fix this issue 
for (lead ulds : [select id,postalcode,state,country from lead where id = '00Q180000029Ujz' limit 1])
 {
   ulds.postalcode = '6';
   update ulds;
 }        
   
lead ld = [select id,postalcode,state,country from lead where id = '00Q180000029Ujz' limit 1];

      system.debug(ld.id);
      system.debug(ld.postalcode);
      system.debug(ld.state);
      system.debug(ld.country);
    
    Integer intzip;
    intzip=Integer.valueof(ld.postalcode);
      
     system.debug(intzip);
    
    Territory_Lookup__c tl = [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
    
                             where Zip_Start__c <= :intzip and Zip_End__c >= :intzip];
     system.debug('Theater :' + tl.Theater__c);
     system.debug('Region :' + tl.Region__c);
     system.debug('Area :' + tl.Area__c);
     system.debug('User :' + tl.User__c);
    
    list<lead> led = [select id,Territory_Area__c,Territory_Region__c,Territory_Theater__c   
                      from lead where id = '00Q180000029Ujz' limit 1]; 
      
     for (lead lds : led)
     {
      lds.Territory_Area__c = tl.Theater__c;    
      lds.Territory_Region__c = tl.Region__c;
      lds.Territory_Theater__c = tl.Area__c;   
         update lds;
       }

Thanks
Sudhir
David ZhuDavid Zhu
I would suspect if the trigger is set to 'active'
sudhirn@merunetworks.comsudhirn@merunetworks.com
Hi David trigger is active am doing a soql from custom object FROM Territory_Lookup__c everytime when pincode is change can you please provide me what might be the mistake 
David ZhuDavid Zhu

How about change line 28 to 39 to test if it works.

lead led = [select id,postalcode,state,country from lead where id = :Trigger.newMap.keySet() limit 1];
led.Territory_Area__c = tl.Area__c;
led.Territory_Region__c = tl.Region__c;
led.Territory_Theater__c = tl.Theater__c;
update led;
sudhirn@merunetworks.comsudhirn@merunetworks.com

Hi David, 
 
   I am hardcoding the lead id to test it in developer console its working fine. I need to convert this into after insert and after update events. 

   Reason I need this is because as user changes the value it must automatically update to that value let me know if my requirement is not clear will explain in another post with code 

Thanks

Sudhir

David ZhuDavid Zhu
I understand hardcode work in anomymose apex. I meant to change line 28 to 39 in the trigger which is in your first post.
reymagdaongreymagdaong
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>(); 

 Integer zipLowerBound = 0;
 Integer zipUpperBound = 0;
 
 for(Lead l : (Lead) Trigger.new){
  gstate.add(l.state);
  gcountry.add(l.country);
  gzip.add(Integer.valueof(l.postalcode));
 }

 gzip.sort(); //sort the list from lowest to highest
 zipLowerBound = gzip.get(0); //get the lowest
 zipUpperBound = gzip.get(gzip.size() - 1); //get the highes value
 
 territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where Zip_Start__c >= :zipLowerBound and Zip_End__c <=:zipUpperBound and
                                 State__c in :gstate and Country__c in :gcountry];
  
  for(lead uld : (Lead) 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){
    Integer zip = Integer.valueof(lead.postalcode);
    if(territoryLookup.State__c == lead.state &&
      territoryLookup.Country__c == lead.country &&
      territoryLookup.Zip_Start__c <= zip&&
      territoryLookup.Zip_End__c >= zip ){
      temp =  territoryLookup;
      break;
    }
  }
  return temp;
 }
}
hi, i tried to refactor your code so that it is bulkified. see if this is correct, haven't tried to compile this so you may find some compile error which you should be able to resolve.
This was selected as the best answer
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks for you reply 

 I am getting below error while saving the class file  Please suggest how to change 

Error: Compile Error: unexpected token: 'Territory_Lookup__c' at line 1 column 7
public Territory_Lookup__c getTerritoryLookup(Lead lead){
  if(territoryLookupList == null)
    return null;
  Territory_Lookup__c temp = null
  for(Territory_Lookup__c territoryLookup : territoryLookupList){
    Integer zip = Integer.valueof(lead.postalcode);
    if(territoryLookup.State__c == lead.state &&
      territoryLookup.Country__c == lead.country &&
      territoryLookup.Zip_Start__c <= zip&&
      territoryLookup.Zip_End__c >= zip ){
      temp =  territoryLookup;
      break;
    }
  }
  return temp;
 }
}

Thanks
Sudhir
reymagdaongreymagdaong
hi, do not save it on a separate class, save it as it is from my previous code, it should belong inside the trigger code itself.
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks I tried also with trigger I am getting below error 

Error: Compile Error: unexpected token: public at line 40 column 1
reymagdaongreymagdaong
try to remove } from line 37.
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks I modified you code to remove all the error below code is working fine. 

Let me explain my requirement again. In lead when ever user changes the ping code or state or country trigger should be able to lookup from object Territory_Lookup__c and display region area and theater information please suggest me how to get this real time as and when user updates the value in lead please suggest me looks like this should be achieved from after insert and after update concept 

 
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>(); 

 Integer zipLowerBound = 0;
 Integer zipUpperBound = 0;
 
 for(Lead l :   Trigger.new){
  gstate.add(l.state);
  gcountry.add(l.country);
  gzip.add(Integer.valueof(l.postalcode));
 }

 gzip.sort(); //sort the list from lowest to highest
 zipLowerBound = gzip.get(0); //get the lowest
 zipUpperBound = gzip.get(gzip.size() - 1); //get the highes value
 
 territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where Zip_Start__c >= :zipLowerBound and Zip_End__c <=:zipUpperBound and
                                 State__c in :gstate and Country__c in :gcountry];
  
  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){
    Integer zip = Integer.valueof(lead.postalcode);
    if(territoryLookup.State__c == lead.state &&
      territoryLookup.Country__c == lead.country &&
      territoryLookup.Zip_Start__c <= zip&&
      territoryLookup.Zip_End__c >= zip ){
      temp =  territoryLookup;
      break;
    }
  }
  return temp;
 }
}


Thanks

Sudhir

reymagdaongreymagdaong
Hi,
Yes i think i understand the requirement, and already created the code/trigger to achieve that behaviour. ther thing is, you cannot do it via AFTER trigger because it will have an 'infinite call' on the trigger, that's why i had it on BEFORE trigger. Please try to review the code. thanks.
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks What am looking is when user updates postalcode it must look in territor object and return a value I though this is only by after update tried you code its not working for some reasons 
 
Try
{
 for (lead ulds : [select id,postalcode,state,country from lead where id = '00Q180000029Ujz' limit 1])
 {
   ulds.postalcode = '30';
   update ulds;
 }        
   
lead ld = [select id,postalcode,state,country from lead where id = '00Q180000029Ujz' limit 1];

      system.debug(ld.id);
      system.debug(ld.postalcode);
      system.debug(ld.state);
      system.debug(ld.country);
    
    Integer intzip;
    intzip=Integer.valueof(ld.postalcode);
      
     system.debug(intzip);
    
    Territory_Lookup__c tl = [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
    
                             where Zip_Start__c <= :intzip and Zip_End__c >= :intzip];
     system.debug('Theater :' + tl.Theater__c);
     system.debug('Region :' + tl.Region__c);
     system.debug('Area :' + tl.Area__c);
     system.debug('User :' + tl.User__c);
    
    list<lead> led = [select id,Territory_Area__c,Territory_Region__c,Territory_Theater__c   
                      from lead where id = '00Q180000029Ujz' limit 1]; 
      
     for (lead lds : led)
     {
      lds.Territory_Area__c = tl.Theater__c;    
      lds.Territory_Region__c = tl.Region__c;
      lds.Territory_Theater__c = tl.Area__c;   
         update lds;
       }   
}

Catch (Exception e)
{
 system.debug('Lookup no found'); 
    }


Above code is working fine in developer console 

Thanks

Sudhir

sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks for you code I tweeked you code its working fine I added futher more filters based on comination values to get desired result 

I am getting Please suggest me how to fix this issue

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger territory_lookup caused an unexpected exception, contact your administrator: territory_lookup: execution of BeforeUpdate caused by: System.NullPointerException: Argument cannot be null.: ()
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>(); 

 Integer zipLowerBound = 0;
 Integer zipUpperBound = 0;
 
 for(Lead l :   Trigger.new){
  gstate.add(l.state);
  gcountry.add(l.country);
  gzip.add(Integer.valueof(l.postalcode));
 }

 gzip.sort(); //sort the list from lowest to highest
 zipLowerBound = gzip.get(0); //get the lowest
 zipUpperBound = gzip.get(gzip.size() - 1); //get the highes value
 
 if ( gzip != null && gstate !=  null && gcountry != null )
 {
 territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where Zip_Start__c <= :gzip and Zip_End__c >=:gzip and 
                                 State__c in :gstate and
                                 Country__c in :gcountry limit 1];
 } 
 else if ( gzip != null && gstate !=  null  &&  gcountry == null)
 {
  territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where Zip_Start__c <= :gzip and Zip_End__c >=:gzip and 
                                 State__c in :gstate limit 1];
  } 
 else if ( gzip != null && gstate ==  null  &&  gcountry == null)
 {
  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 == null && gstate ==  null  &&  gcountry != null)
 {
  territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where Country__c in :gcountry limit 1];
  }                            
 else if ( gzip != null && gstate !=  null  &&  gcountry == null)
 {
  territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where Zip_Start__c <= :gzip and Zip_End__c >=:gzip and State__c in :gstate limit 1];
  }
 else if ( gzip != null && gstate !=  null  &&  gcountry == null)
 {
  territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where State__c in :gstate and Country__c in :gcountry limit 1];
  }
 else if ( gzip != null && gstate ==  null  &&  gcountry == null)
 {
  territoryLookupList =  [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c
                           where  Zip_Start__c <= :gzip and Zip_End__c >=:gzip and  Country__c in :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){
    Integer zip = Integer.valueof(lead.postalcode);
   
      temp =  territoryLookup;
      break;
    
  }
  return temp;
 }
   
}

Thanks
Sudhir
reymagdaongreymagdaong
Hi,
Please replace this
Zip_Start__c <= :gzip and Zip_End__c >=:gzip
with
Zip_Start__c <= :upperBound and Zip_End__c >=:lowerBound
gzip is a list and the zip comparision should be by integer thats why i created those 2 variables.

and also please remove the limit 1 on all queries,

regards,
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks I did that problem am facing is I am adding a conditional SOQL based on the values we have in lead 
  
  For example if lead dont have zip have only country I am trying to get the soql belonging to that country if lead have only zip try to lookup to get this value.

 Problem now is when I change country from one value to another its not updating when run the trigger for multiple times its working please suggest me how to fix this issue. 

  Do we have to change the event of trigger to after update need your suggestion 

Thanks
Sudhir
sudhirn@merunetworks.comsudhirn@merunetworks.com
Hi I tried to tweek you code for some reasons only if zip is there it works perfect if we make zip as null and try with country or state value its not working trigger has to fire multiple times 

Below is the code which i modifed as you suggested for some reasons only zip works perfect please suggest me for combination values like if zip i null and country only exist it must be able to pick value from the territory list and return 
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
sudhirn@merunetworks.comsudhirn@merunetworks.com
Hi rey magdaong,

 Can you please help me with your code I am stuck in this trigger with you logic 

 Trigger below works perfect with zip code filters 
 
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));
   }
 }
 
  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]; 


  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;
 }
   
   
}

Same trigger am adding only country filter its not working real time when we make a update its working only when multiple times it is updated. Please suggest me what is the issue in the logic
 
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));
   }
 }
 
  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;
 }
   
   
}

 Need you suggestion its valuable for me now 

Thanks
Sudhir