You need to sign in to do that
Don't have an account?
Gita Borovsky
Sudden "Too many SOQL queries" Error
Hi All,
This trigger has been running just fine with no no errors, then yesterday starting throwing back "UpdateLeadOwner: System.LimitException: Too many SOQL queries: 101"
Here's the code:
trigger UpdateLeadOwner on Lead (before insert,before update) {
for (Lead leadInLoop : Trigger.new) {
if (leadInLoop.LeadSource != 'Contact Us' && leadInLoop.LeadSource != 'Reprint') {
// Retrieve owner from Account record based on email domain name
list <Account> acct = [Select OwnerId from Account WHERE Domain__c = :leadInLoop.Domain__c AND Owner.UserRoleId != '00E50000000t0Bf' Limit 2];
if (acct.size() == 1) {
System.debug('account owner id is: ' + acct[0].OwnerId);
leadInLoop.OwnerId=acct[0].OwnerId;
}
}
}
Any idea how to track down what changed so I can fix? Thanks!
This trigger has been running just fine with no no errors, then yesterday starting throwing back "UpdateLeadOwner: System.LimitException: Too many SOQL queries: 101"
Here's the code:
trigger UpdateLeadOwner on Lead (before insert,before update) {
for (Lead leadInLoop : Trigger.new) {
if (leadInLoop.LeadSource != 'Contact Us' && leadInLoop.LeadSource != 'Reprint') {
// Retrieve owner from Account record based on email domain name
list <Account> acct = [Select OwnerId from Account WHERE Domain__c = :leadInLoop.Domain__c AND Owner.UserRoleId != '00E50000000t0Bf' Limit 2];
if (acct.size() == 1) {
System.debug('account owner id is: ' + acct[0].OwnerId);
leadInLoop.OwnerId=acct[0].OwnerId;
}
}
}
Any idea how to track down what changed so I can fix? Thanks!
To fix this, you'll need to utilize a separate list or map, and move your SOQL outside of the for loop.
Below is an example of how you might remedy it. You'll have to test the solution out yourself, of course.
https://developer.salesforce.com/page/Apex_Code_Best_Practices
Also, are there any other triggers on your Lead object?
I would recommend using a Map to update the Lead Owner.
Here is some code to get you started. Here, even if you have 200 leads being updated, you only make 1 SOQL query. Before you would make a SOQL query for each Lead record.