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
Cris9931Cris9931 

too many sql queries: 101

Hi, I have this simple trigger and I'm not sure which line is causing the problem...:

trigger SIG_Trigger_IT_WO on SVMXC__Service_Order__c (before insert, before update) {


    Set<Id> srId = new Set<Id>();
    List<SVMXC__Service_Request__c> srToUpdate= new List<SVMXC__Service_Request__c>();
    
        for(SVMXC__Service_Order__c wos : Trigger.New){
            srId.add(wos.SVMXC__SM_Service_Request__c);
        }
        
        List<SVMXC__Service_Request__c> sr= [select id, Number_of_Open_Work_Orders__c from SVMXC__Service_Request__c where Id in :srId ];
        List<SVMXC__Service_Order__c > wos= [select id, SVMXC__Order_Status__c from SVMXC__Service_Order__c where (SVMXC__Order_Status__c != 'Closed' AND SVMXC__Order_Status__c != 'Cancelled') AND SVMXC__SM_Service_Request__c in :srId  ];
        for(SVMXC__Service_Request__c s : sr){
            s.Number_of_Open_Work_Orders__c = wos.size();
           srToUpdate.add(s);
        }
        update srToUpdate;


}

Can someone help me?
Best Answer chosen by Cris9931
Cris9931Cris9931

https://www.appseconnect.com/recursive-trigger-in-salesforce/#:~:text=The%20Recursive%20trigger%20is%20a,how%20it%20can%20be%20avoided.&text=The%20trigger%20calls%20a%20function%20called%20'Call'.

 

this helped me. :) seems like it's a reccursive thing.

All Answers

SwethaSwetha (Salesforce Developers) 
HI Cristian,

You need to setup debug logs to see the stack trace of SOQL that is throwing this error.

There can be multiple reasons for this exception:
 Check if SOQL queries are not written in loops
Check if DML is not called inside a loop.
Check if a large number of workflows for field updates are not created on the same object, if possible, move the field update code to trigger.

In your code, I cannot see SOQL calls inside of loops, so there is a good chance there is a Trigger or Process Flow working on the background which is not "Bulkified", thus causing the limit to be hit.

Related:
https://help.salesforce.com/articleView?id=000331875&type=1&mode=1
https://blog.webnersolutions.com/salesforce-system-limitexception-too-many-soql-queries-101/

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
AnudeepAnudeep (Salesforce Developers) 
Error: System.LimitException: Too many SOQL queries: 101 comes when you hit Governors Limit 

Please check this post to avoid limit

I recommend checking how many queries have run so far using the following and optimizing the code
 
System.debug('Total Number of SOQL Queries allowed in this apex code context: ' + Limits.getLimitQueries());
System.debug('Total Number of records that can be queried in this apex code context: ' + Limits.getLimitDmlRows());
System.debug('Total Number of DML statements allowed in this apex code context: ' + Limits.getLimitDmlStatements() );
System.debug('Total Number of script statements allowed in this apex code context: ' + Limits.getLimitScriptStatements());

USAGE
System.debug('1.Number of Queries used in this apex code so far: ' + Limits.getQueries());
System.debug('2.Number of rows queried in this apex code so far: ' + Limits.getDmlRows());
System.debug('3. Number of script statements used so far : ' + Limits.getDmlStatements());
System.debug('4.Number of Queries used in this apex code so far: ' + Limits.getQueries());
System.debug('5.Number of rows queried in this apex code so far: ' + Limits.getDmlRows());

Let me know if this helps
Cris9931Cris9931

https://www.appseconnect.com/recursive-trigger-in-salesforce/#:~:text=The%20Recursive%20trigger%20is%20a,how%20it%20can%20be%20avoided.&text=The%20trigger%20calls%20a%20function%20called%20'Call'.

 

this helped me. :) seems like it's a reccursive thing.

This was selected as the best answer