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
Ap30Ap30 

can someone give example code to generate 101 error using for loop in one transaction. And same can be solved using @future

I'm new to this concept. Please help
PriyaPriya (Salesforce Developers) 
Hi 

“System.LimitException: Too many SOQL queries: 101” errors occur when you exceed SOQL queries governor limit. The Actual limit is “you can run up to a total 100 SOQL queries in a single call or context”.

To learn more about the Governor limit, kindly refer this :- 

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

Ho to resolve this “Error: System.LimitException: Too many SOQL queries : 101”:- 

A. Change your code by following apex code best practices so that the number of SOQL fired is less than 100.
1.Avoid SOQL queries that are inside FOR loops.
2.Make sure you don't have a recursive loop calling a SOQL.
3.Do not do any DML/CRUD inside a for loop.
4.Avoid more than one DML on single object in single transaction - because each DML invokes trigger of related object can cause addition of SOQL count.
5.Avoid too many field updates in process builders in different conditional branches. Each branch runs a DML and initiate trigger execution.
6.If there are lot of business processes and logic in your trigger then design and isolate processes with help of future and batch if needed.
7.Recursion Handling - To ensure a trigger is not running recursively.

Kindly refer this for best pracitces :-  https://developer.salesforce.com/wiki/apex_code_best_practices

B.  If you need to change the context, you can use @future annotation which will run the code asynchronously.

To learn more about the future method, you can refer this :- 

https://developer.salesforce.com/forums/?id=906F0000000QtsRIAS#:~:text=For%20example%2C%20you%20can%20use,callout%20to%20an%20external%20service.&text=Instead%20of%20a%20Schedulable%20class,and%20can%20call%20%40future%20methods.&text=For%20more%20information%20refer%20to%20the%20below%20link.

For more examples, refer below link :- 
https://salesforce.stackexchange.com/questions/141297/error-occurred-too-many-soql-queries-101-how-do-i-fix

https://blog.salesforcecasts.com/too-many-soql-queries-101/

 

If the above Information helps,Please mark it as Best Answer so that it can help others in the future.

Regards,

Priya Ranjan