You need to sign in to do that
Don't have an account?
BobbyQ
Help with First Apex Trigger: Too many SOQL queries: 101
Hello, I am getting the following error message. This is my first Apex Trigger and would really appreciate some help. The trigger looks up the lead postalcode in a custom object of zip codes and territory information and populates a lookup field on the lead with the record id of the corresponding zip code in the custom object.
Are there any changes I can make to elimate this error? Here is the code:
System.LimitException: Too many SOQL queries: 101
Are there any changes I can make to elimate this error? Here is the code:
1trigger LeadAssignmentTrigger on Lead (before insert, before update) 2{ 3 List<Lead> leadsToUpdate = new List<Lead>();3 4 5 for (Lead lead : Trigger.new) 6 { 7 if (lead.PostalCode != NULL) 8 { 9 // Find the sales rep for the current zip code 10 List<Territory_Zip_Codes__c> zip = [select id from Territory_Zip_Codes__c 11 where Name = :lead.PostalCode limit 1]; 12 13 // if you found one 14 if (zip.size() > 0) 15 { 16 //assign the lead owner to the zip code owner 17 lead.Territory_Zip_Code__c = zip[0].id; 18 19 leadsToUpdate.add(lead); 20 21 } 22 } 23 } 24}
better would be to build a list of Postcode values in a list, then run the query, then process the results.
The above should put you on the right path.
Regards
Andrew
note: all code provided uncompiled and as-is
All Answers
better would be to build a list of Postcode values in a list, then run the query, then process the results.
The above should put you on the right path.
Regards
Andrew
note: all code provided uncompiled and as-is
til next time
Andrew