The error message "System.LimitException: Too Many SOQL Queries: 101" indicates that your Salesforce Apex code or process has exceeded the maximum number of SOQL queries that can be executed in a single transaction. Salesforce enforces limits to prevent resource abuse and to ensure fair usage by all customers.
Explanation:
SOQL Queries Limit: In Salesforce, there's a limit on the number of SOQL queries you can perform in a single transaction. As of my last update, the limit is 100 SOQL queries per transaction.
Common Causes:
Here are some common reasons why you might encounter this error:
- Nested or Recursive Queries: Your code might be executing nested or recursive queries, causing multiple queries to be executed within a single transaction.
- Queries in Loops: SOQL queries executed inside loops can quickly accumulate and exceed the limit, especially if the loop iterates over a large number of records.
- Trigger Logic: If you have triggers on objects that perform additional database operations (such as querying related records), they can contribute to exceeding the SOQL query limit.
Solutions:
To address this error, consider the following solutions:
- Bulkify Your Code: Refactor your code to process records in bulk rather than individually. This reduces the number of queries needed to perform the same operation.
- Query Optimization: Review your code and identify areas where you can optimize SOQL queries, such as reducing the number of queries executed or leveraging relationships to fetch related data in fewer queries.
- Avoid Queries in Loops: Ensure that your code does not perform SOQL queries inside loops. Instead, query the required data outside the loop and use collections to process records efficiently.
- Use Limits Methods: Utilize Salesforce's Limits methods (Limits.getQueries() and Limits.getLimitQueries()) to monitor and control the number of SOQL queries being executed in your code.
- Consider Asynchronous Processing: If your code requires complex processing that exceeds the limits, consider using asynchronous processing techniques such as batch Apex or queueable Apex to execute the logic in separate transactions.
Example:
Here's an example of how you might encounter this error in Apex code:List accounts = [SELECT Id, Name FROM Account LIMIT 1000];
for(Account acc : accounts) {
// Perform some logic that executes a SOQL query
List contacts = [SELECT Id, Name FROM Contact WHERE AccountId = :acc.Id];
// Process contacts
}
In this example, if the accounts list contains more than 100 records, the inner SOQL query inside the loop will exceed the SOQL query limit, resulting in the "Too Many SOQL Queries" error.
Conclusion:
The "System.LimitException: Too Many SOQL Queries: 101" error indicates that your Apex code has exceeded the maximum number of allowed SOQL queries in a single transaction. By optimizing your code, avoiding unnecessary queries, and following best practices, you can prevent this error and ensure efficient use of Salesforce resources.