System.LimitException: Too many SOQL queries: 101 error

System.LimitException: Too many SOQL queries: 101 error

As a Salesforce developer, encountering the “System.limitException: Too many SOQL Queries” error is a common challenge. This error occurs when the number of Salesforce Object Query Language (SOQL) queries executed in a single transaction exceeds the governor limits set by Salesforce.

In this blog, we will walk you through the various aspects of this error and provide you with effective strategies to overcome it.

SOQL stands for Salesforce Object Query Language. It’s a special query language used in Salesforce to search for specific information in Salesforce org. It’s like asking Salesforce questions about your data and it helps you find the answers you need.

Salesforce puts limitations on its system to make sure it runs smoothly for everyone. These limitations apply to how much data you can look up or change at once. There are limits on searches (SOQL queries), updates (DML statements), and how long it takes to process them (CPU time).

Knowing these limitations is important so you don’t run into errors. One important limit is executing only 100 SOQL queries per transaction. There are also limits on how many records you can get in a single search and how many related records you can look up at once. By understanding these limitations, you can write better code and avoid errors.

Causes of this error:

Let’s explore why this happens and how to avoid it.

  • If you use SOQL queries inside a FOR loop, each loop cycle might trigger a new query. This can quickly add up and hit the limit, causing the SOQL 101 error.
  • If a trigger isn’t set up to handle large amounts of records efficiently, it might fire off too many queries and cause errors.
  • Things can get even trickier with recursive triggers. These are triggers that call themselves again during processing.
  • Finally, be mindful of concurrent updates. If multiple processes try to update the same record at the same time, they might each fire off queries. This competition for resources can also lead to the SOQL 101 error.

By understanding these common causes, you can write error-free code.

How to resolve this error?

1. Avoid using SOQL in the FOR loop

It is important to avoid placing SOQL queries or DML operations inside a FOR loop. This is because such operations will execute multiple times, once for each iteration of the loop. Doing this can quickly result in hitting Salesforce’s governor limits.

Want to Learn Salesforce Flows? Checkout our Salesforce Flow Course

2. Bulkify Triggers

Improve trigger logic to efficiently handle large datasets without exceeding query limits.

Minimize the use of data manipulation language (DML) operations by first adding records to collections and then executing DML operations on these collections.

It’s best to minimize the number of SOQL statements. You can achieve this by pre-processing records and creating sets, which can then be used in a single SOQL statement with the IN clause.

3. Use @future method

Executing Apex within an @future method operates under a distinct set of elevated limits provided by Salesforce. For instance, the allowable number of SOQL queries rises from 100 to 200 when employing asynchronous calls.

Furthermore, the total heap size and maximum CPU time expand proportionally for asynchronous operations. So, Transfer some of your business logic into @future methods.

4. Refer Apex Developer guide

It’s important to follow the coding rules outlined in the Developer’s Guide when writing Apex code. These rules serve as basic guidelines to help you create reliable code without hitting the governor limits.

Conclusion:

Knowing and handling the “Too many SOQL Queries” error in Salesforce is important. By learning about Salesforce limits, and following simple rules for queries you can fix this. Keep an eye on your code to follow the rules.

Also Read- You can’t disable “View Setup and configuration” in this profile because it’s assigned to a user who is a delegated administrator

FAQs

1. What are the governor limits associated with SOQL queries in Salesforce?

Salesforce imposes limits on the number of SOQL queries per transaction, the total number of records retrieved and the complexity of queries.

2. How can we ensure my SOQL queries are optimized to avoid hitting governor limits?

To optimize SOQL queries, limit the number of fields and records returned, use selective filtering criteria, and minimize the use of relationship traversals.

3. Is there a maximum limit on the number of characters allowed in a SOQL query string?

Yes, Salesforce imposes a maximum limit on the size of a SOQL query string, including all keywords, fields, conditions and clauses.

4. How many subqueries can be written in SOQL?

The synchronous limit allows issuing a total of 100 SOQL queries, while the limit for subqueries is set at 300.

Get a complete Roadmap To Learn Salesforce Admin And Development

Share Now

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *