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
Nicholas PalattaoNicholas Palattao 

Most common causes of "Too many SOQL queries: 101" error message

This is more of a general question than anything, but I recently inherited a Salesforce admin/developer position at this company and there is a high amount of clutter and errors throughout the system. What I've noticed more than anything else is the "Too many SOQL queries:101" error. It seems to stem most often from conflicts between process builder workflows and apex classes/triggers currently in production. The issue is that I did not personally write any of these classes or triggers and the error messages don't exactly give a clear solution. Basically, what are the most common causes of this error and what is (most often) the best way to resolve it?
Santosh Kumar 275Santosh Kumar 275
Look SOQL basically stands for Salesforce Object Query Language (SOQL).SOQL is similar to the SELECT statement in the widely used Structured Query Language (SQL) but is designed specifically for Salesforce data.
For example : SELECT Id, Name FROM Account WHERE Name = 'XYZ'.
So basically whenever you write a query in Salesforce it is counted under SOQL.

And you must be aware of that in Salesforce we are having governor limits.
What are Governor Limits?
As we know, Apex runs in multi-tenant environment; i.e. a single resource is shared by all the customers and organizations. So, it is necessary to make sure that no one monopolizes the resources and hence Salesforce.com has created the set of limits which governs and limits the code execution. Whenever any of the governor limits are crossed, it will throw error and will halt the execution of program.
From a Developer's perspective, it is of utmost important to ensure that our code should be scalable and should not hit the limits.
All these limits are applied on per transaction basis. A single trigger execution is one transaction.

Refer : https://developer.salesforce.com/docs/atlas.en-us.salesforce_app_limits_cheatsheet.meta/salesforce_app_limits_cheatsheet/salesforce_app_limits_platform_apexgov.htm

Now coming back to your question : 
You could issue only 100 queries per transaction, that is when your code will issue more than 100 SOQL queries then it will throw error.
 
Now to avoid this you should follow best practices for writing and designing Apex Code.
One best practices is to Avoid SOQL Queries or DML statements inside FOR Loops because if will write query inside for loop then after every 100 iterartion the limits will hit and you will get an error 101 stating "Too many SOQL queries: 101".

Refer : https://developer.salesforce.com/page/Apex_Code_Best_Practices  for apex best practices.

Hope this will help you. Feel free to ask if you have doubts.

If this answer serves you need then Mark it as Best answer.
Nicholas PalattaoNicholas Palattao
So say, for example, one record is edited and saved. This would trigger all triggers and workflows related to the object, and each trigger would be allocated a max of 100 SOQL queries for itself, correct? When multiple workflows from the process builder are triggered on a record edit, is each workflow considered a transaction and thereforce each workflow has its own limit of 100 queries? It just seems as if this limit is being applied to multiple flows/triggers at once, given how often this error comes up.
Santosh Kumar 275Santosh Kumar 275
No, the limit applies for one transaction.
All the triggers fired will be counted in a single context or call. To fix the issue, you'll need to change your code in such a way that SOQL fired is less than 100.
If you need to change the context then you can use @future annotation which will run the code asynchronously.
Malika Pathak 9Malika Pathak 9

Hi Nicholas,

Find the below solution,

This error is telling you that the SOQL on line 3 is the SOQL that puts you over the inhibition.  It doesn't betoken that is where the quandary is.  The circumscription applies to ALL code being run during the operation, your just transpires to be the 'one an extravagant quantity'.    

Utilize the Debug Logs or Developer Console to visually perceive the SOQL executions during the operation to ascertain what other code is running.

However, the code you gave us is a class, which designates it is called from somewhere. If this class is being called from inside a trigger loop, it will be called once for each record in the trigger.

I am referring to some Links please check refer these links

https://www.salesforcetutorial.com/explain-many-soql-queries/#:~:text=LimitException%3A%20Too%20many%20SOQL%20queries%3A%20101%E2%80%9D%20errors%20occur%20when,a%20single%20call%20or%20context%E2%80%9D.&text=Change%20your%20code%20by%20following,fired%20is%20less%20than%20100.

If you find this helpful mark it as the best answer.