Thursday, May 26, 2016

Developer: Too many SOQL queries....

When you hit this error; standard best practice will ask you to review the following:

1. Since Apex runs on a Multitenant structure, Apex runtime engine strictly enforces limits to ensure code doesn't monopolize shared resources. Learn about the Governors Limit.
2. Avoid SOQL queries that are inside FOR loop.
3. Check out the Salesforce Developer Blog where you can find Best Practices for Triggers.
4. Review best practices for Trigger and Bulk requests on our Force.com Apex Code Developer's Guide.
5. Be sure you're following the key coding principals for Apex Code in our Developer's Guide.


Sometimes the answer is not that obvious that you review a piece of code and you find the solution.

Here's my list of steps:

Review your WF/Process Builder rules; many times Admins write basic updates on the same entity causing continous firing of Before/After triggers. Try to move them to code.

Review the WF rules and move them to formulas; check for ISCHANGED so that the rules are not fired perpetually.

If this too doesn't solve your issues; turn the debug logs on. Look for SQL_EXECUTE_BEGIN.  Salesforce does a good job providing the query num that is being fired. Try to observe the pattern; atleast one query or process is being fired repeatedly. Work on that process or query; typically making it @future has helped solve the issue. However, if you are in an environment where the code is not being written by you or jumps from class to class that keeping track can become challenging and confusing use the log route to zero in on your problem.

No comments:

Post a Comment