+ Start a Discussion
Malisa Gibbons 9Malisa Gibbons 9 

Can I get help writing a trigger to assign leads by zip code? I do not know how to do this.

Best Answer chosen by Malisa Gibbons 9
RamakantRamakant
HI Malisa,

You can achieve this using Custom Setting and trigger.
Create a custom setting Lets Say Name is ZipAndUsers with fields Name(Text - Put yor ZipCode in this field) and create new Custom Field for UserId(Id OR Text).Load your custom setting data using dataloader .
This code should work for you. PLease change your custom setting name and field names as required:
trigger assignOwner on Lead (before insert, before update) {
Map<Id, Delivery__c> mapOfDeliveryRecords = new Map<Id, Delivery__c>();
    List<Item_Distributed__c> lstItemDestriBute = new List<Item_Distributed__c>();
	Map<String,CustomSettingName__c> allCodes = CustomSettingName__c.getAll();
    for(Lead led : Trigger.new) {
		if(allCodes.containsKey(led.ZipCodeField)){
			led.OwnerId = allCodes.get(led.ZipCodeField).UserIdField;
		}
    }
}

PLease mark as best answer if helped.

Regards,
Ramakant​

 

All Answers

RamakantRamakant
Hi Malisa,

As per my knowledge you don't need to write trigger to achieve this. Lead assignment rules could do the trick.

Please let me know if you need more help.

Regards,
Ramakant
Vinod ChoudharyVinod Choudhary
Hi Malisa,

there is a solution for this on comminity.
https://success.salesforce.com/answers?id=9063A000000iUJ4QAM

Hope this will help you :)

Thanks
Vinod
Malisa Gibbons 9Malisa Gibbons 9
I found that yesterday but don't know how to write it.  Can I get assistance in building this?
Malisa Gibbons 9Malisa Gibbons 9
Ramakant - I have all the zip codes in the US - using the lead assignment rule would take forever..Is there an easy way to do this?
Vinod ChoudharyVinod Choudhary
Hi Malisa,

I am sorry but currenly i can not write whole steps to create this and write trigger for you. 
also it's not possible to do it without login in your ORG.

you need to find someone (Saqlesforce Developer) near you who can code for you. :)

Thanks
Vinod
 
RamakantRamakant
HI Malisa,

You can achieve this using Custom Setting and trigger.
Create a custom setting Lets Say Name is ZipAndUsers with fields Name(Text - Put yor ZipCode in this field) and create new Custom Field for UserId(Id OR Text).Load your custom setting data using dataloader .
This code should work for you. PLease change your custom setting name and field names as required:
trigger assignOwner on Lead (before insert, before update) {
Map<Id, Delivery__c> mapOfDeliveryRecords = new Map<Id, Delivery__c>();
    List<Item_Distributed__c> lstItemDestriBute = new List<Item_Distributed__c>();
	Map<String,CustomSettingName__c> allCodes = CustomSettingName__c.getAll();
    for(Lead led : Trigger.new) {
		if(allCodes.containsKey(led.ZipCodeField)){
			led.OwnerId = allCodes.get(led.ZipCodeField).UserIdField;
		}
    }
}

PLease mark as best answer if helped.

Regards,
Ramakant​

 
This was selected as the best answer