+ Start a Discussion
GRStevenBrookesGRStevenBrookes 

Too many SOQL queries : 101 -Arghhhhhh!

Hi, 

 

Please could someone help me my telling me why I am gettin the Too Many SOQL Queries Error on the following trigger:

 

trigger CountAccountType_Customer on Account (after delete, after insert, after undelete, 
after update) {

     Set<Id> esIds=new Set<Id>();
     for (Account es : trigger.new)
     {
        esIds.add(es.OwnerId);
     }

     List<User> sis=[select id, of_Accounts_with_Type_Customer__c, (select id from Accounts__r) 
     from User where id in :esIds];
    
     for(User si : sis) {
      
      si.of_Accounts_with_Type_Customer__c  = si.Accounts__r.size();
  }
  update sis;
}

 

Starz26Starz26

roll up summary on account?

Another trigger?

 

Review flow in debug logs and see what else is firing right after the line for  UPDATE SIS

sfdcfoxsfdcfox

Starz is right. For clarification, the govenor limit was exceeded at this trigger's execution, but isn't the cause of the problem. Either another function performed a DML that caused this trigger to run when SOQL queries was already at 100, or this trigger was recursively called because of some other trigger that was misbehaving and caused this error. Either way, you need to look for either a DML-inside-loop problem, or a SOQL-inside-loop problem:

 

// DML inside loop issue
for(...) {
   update ...; // or delete, or insert, or upsert...
}
// Fixed
list<sobject> recs;
for(...) {
    recs.add(...);
}
update recs; // or delete, or insert, or upsert...

 

// SOQL inside loop
for(...) {
   sobject x = [select ... where field__c = :some_value];
}
// Fixed
set<object> some_values ...;
for(...) {
   some_values.add(...);
}
map<object,sobject> some_map = new map<object,sobject>();
for(sobject x:[select ... where field__c in :some_values]) {
   some_map.put(x.field__c,x);
}
for(...) {
   sobject x = some_map.get(...);
}

Look at the docs for more details on this. Also, try running a free security scan at http://security.force.com/sourcescanner. It will outline errors in your code, such as DML or SOQL inside loops problems.