Actually, I am trying to get Area_c
field from custom object(Zip Codes) into the lead object custom field Area_c
. So, I have written some of the code but is there any other way to write it or get some optimization of this code.
trigger OpportunityPriceBook on Lead (before insert) {
List<String> zipList = new List<String>();
for(Lead lead : Trigger.new){
zipList.add(lead.PostalCode);
}
List<Zip_Code__c> zipCodeList = [Select Id, Name, City__c, Area__c from
Zip_Code__c WHERE Name IN : zipList];
for(Lead leads : Trigger.new){
for(Integer i =0 ; i < zipCodeList.size(); i++)
{
leads.Area__c = zipCodeList.get(i).Area__c;
leads.City = zipCodeList.get(i).City__c;
break;
}
}
}
Given the code, there are a couple of ways that you can simplify your last multi-dimensional for loops; nested for loops can be problematic at times due to their exponential nature.
That point made, you have one pretty glaring bug in your code: You do not have an identifying conditional statement in your nested for loop, and actually always only set the lead to the first zipcode's data each time.
This is the approach I would take: