function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BrandiTBrandiT 

Really Simple Trigger causing error: Too many SOQL queries: 21

I just tried to deploy a trigger to production and it failed because of another simple trigger I have.  The error message I received was:

Failure Message: "System.LimitException: Too many SOQL queries: 21", Failure Stack Trace: "Trigger.AutoCltGroup: line 2, column 32"

 

The trigger that is causing the error message is really simple, it just populates a lookup field on account if it's null.  I've posted the trigger code below and the line giving the error  is in red.  How can I rewrite it so that I don't get the error messsage?  Please help!!

 

trigger AutoCltGroup on Account (before insert, before update) { 
  LIST<Client_Groups__c> CGs = [Select ID From Client_Groups__c
          Where Name='UNGROUPED'];  

  
  for (Account a : Trigger.new) { 
    if(a.Client_Group__c==null){
      a.Client_Group__c = 'a0F70000005bzBa'; 
    }
  }
}

DharmeshDharmesh

In scope of trigger Salesforce allows only 20 SOQL. Here in your tirgger there is only one query.

but it may possible the that if you combine all the trigger on Account object the SOQL crosses 20.

Check out other triggers wrote on the Account object and SOQL of all triggers on Account object should not cross the limit 20.

 

b-Forceb-Force

It looks some other triggers get executed during the same Execution scope,

 

by using system log -----Monitoring--->Debug Logs (Add user filter )

Check out how many other triggres get Executed

 

Thanks,

Bala

 

 

hisrinuhisrinu

You have to check all the triggers on account to ensure that there are no other triggers which is giving this problem

 

If there are no other triggers then you might be inserting/updating the accounts more than 20 times in one context, due to this you might be getting this error.