What does the flow-related error: System.LimitException: Too many SOQL queries: 101 mean?

918    Asked by DianaCampbell in Salesforce , Asked on Aug 23, 2023

 I have created a flow that can capture the duration in every opportunity stage. But when I try to run the flow, I get the system.LimitException: Too many SOQL queries: 101 error. Even though I know I may have hit the SOQL query limit, I am unable to understand which flow is responsible. Can I get any suggestions as to what it means or how to solve this? 

 The System.LimitException: Too many SOQL queries 101 occurs when you cross the limit of the Execution Governors. It means you can run 100 SOQL queries in one call. You are getting this error because you may have triggered a record and updated the same, which, as a result, triggers the flow creating an infinite loop. It shows the 101 error. You can use the Before Save update flows to use the fast field updates.



Your Answer

Answer (1)

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

4 Months

Interviews

Parent Categories