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
Lance BrownLance Brown 

Lead Assignment Apex Trigger for Zip Codes

We cover every State in the US, and have 100+ salespeople with a very complex territory.  This Salesforce site discusses what we should do, just not who the trigger should look or a sample.(http://help.salesforce.com/apex/HTViewSolution?id=000001140&language=en_US)   I've created the Custome Object and uploaded all of the Zip Cades and linked with the Salesforce User.  I just need help writing the trigger and class.  Any help :)
Best Answer chosen by Lance Brown
Lance BrownLance Brown
Might not be the best looking code, but some how it worked (fyi, im not a coder).  it triggers on insert and update.
TZDatabase is a custom object with every zip code in the US, plus Census data  
__________________________________________________________

trigger LeadAssignmentTrigger on Lead (before insert, before update)
{
    List<Lead> leadsToUpdate = new List<Lead>();

    for (Lead lead : Trigger.new)
    {    
      if (lead.PostalCode != NULL)
      {
          // Find the sales rep for the current zip code
          List<TZDatabase__c> zip = [select Sales_Person__c from TZDatabase__c
                             where Name = :lead.PostalCode limit 1];     
               
          // if you found one
          if (zip.size() > 0)
          {   
              //assign the lead owner to the zip code owner
              lead.OwnerId = zip[0].Sales_Person__c;
         
              leadsToUpdate.add(lead);
         
          }
       }
    }
}

All Answers

AshwaniAshwani
Hi Lance,

That seems a simple approach, you can read zip code of newly created lead and thenfind associated user with that aprticualr zip code. Assign that user's Id to Lead record's owner field (ownerId). A trigger on before insert would work.
Lance BrownLance Brown
Thanks, but writing the Trigger is where I'm running into issues.  Can't figure it out.
Lance BrownLance Brown
Might not be the best looking code, but some how it worked (fyi, im not a coder).  it triggers on insert and update.
TZDatabase is a custom object with every zip code in the US, plus Census data  
__________________________________________________________

trigger LeadAssignmentTrigger on Lead (before insert, before update)
{
    List<Lead> leadsToUpdate = new List<Lead>();

    for (Lead lead : Trigger.new)
    {    
      if (lead.PostalCode != NULL)
      {
          // Find the sales rep for the current zip code
          List<TZDatabase__c> zip = [select Sales_Person__c from TZDatabase__c
                             where Name = :lead.PostalCode limit 1];     
               
          // if you found one
          if (zip.size() > 0)
          {   
              //assign the lead owner to the zip code owner
              lead.OwnerId = zip[0].Sales_Person__c;
         
              leadsToUpdate.add(lead);
         
          }
       }
    }
}
This was selected as the best answer