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
MrBungleMrBungle 

Is there a way to Debug Print the Number of SOQL Queries During Runtime?

I am troubleshooting System.LimitException: Too many SOQL queries: 101 errors in test classes. Is there a way that I can place i.e. system.debug('<current # on SOQL queries>'); to see where in my code that the SOQL queries jump in debug logs? Is there such a way to reference this global SOQL query count in my apex code?

 

Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
Jake GmerekJake Gmerek

Try this:

 

System.debug('Total Number of SOQL Queries allowed in this apex code context: ' +  Limits.getLimitQueries());

 

from this blog post:

 

http://mail.database.com/page/Best_Practice:_Use_of_the_Limits_Apex_Methods_to_avoid_Hitting_Governor_Limits

All Answers

Jake GmerekJake Gmerek

Try this:

 

System.debug('Total Number of SOQL Queries allowed in this apex code context: ' +  Limits.getLimitQueries());

 

from this blog post:

 

http://mail.database.com/page/Best_Practice:_Use_of_the_Limits_Apex_Methods_to_avoid_Hitting_Governor_Limits

This was selected as the best answer
MrBungleMrBungle
Thank you for this pointer. What I needed was Limits.getQueries(); where the Limits.getLimitQueries()); actually returns the limit which is 100.