You need to sign in to do that
Don't have an account?
Lance 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 :)
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
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.
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);
}
}
}
}