How to resolve too many SQL queries errors in Salesforce?

In Salesforce, encountering “Too many SOQL queries” errors usually means that your code is exceeding the governor limit for the number of SOQL queries that can be executed in a single transaction.

Let’s take a common scenario where you might encounter the “Too many SOQL queries” error and provide a sample solution to optimize it.Problem Scenario:You have a trigger on the Contact object that updates related Account records based on some criteria. The inefficient code might look like this:

trigger ContactTrigger on Contact (after update) {
    for (Contact c : Trigger.new) {
        Account acc = [SELECT Id, Name FROM Account WHERE Id = :c.AccountId];
        // Some logic to update the account
        update acc;
    }
}

This code performs a SOQL query inside a loop, which can quickly exceed the governor limit if there are many Contact records being updated at once.

trigger ContactTrigger on Contact (after update) {
    // Step 1: Collect Account IDs
    Set<Id> accountIds = new Set<Id>();
    for (Contact c : Trigger.new) {

Here are some strategies to resolve this issue:

  1. Bulkify Your Code: Ensure that your code is optimized to handle multiple records at a time. This means avoiding SOQL queries inside loops. Instead, gather the necessary record IDs in a collection and perform the query outside the loop.
  2. Use Collections Efficiently: Utilize Maps and Sets to store and retrieve unique records. This can help reduce the number of queries by eliminating the need to query for the same records multiple times.
  3. Query Only What You Need: Be specific in your SOQL queries and retrieve only the fields and records that are necessary for your operation. Avoid using SELECT *.
  4. Use Aggregate Functions: If you’re performing calculations or summaries, consider using aggregate functions (e.g., COUNT, SUM, AVG) in your SOQL queries to reduce the number of records you need to process in Apex.
  5. Limit the Scope of Your Data: If possible, narrow down your query results by using more specific criteria in the WHERE clause. This can help reduce the number of records you need to work with.
  6. Optimize SOQL Query Performance: Ensure that your queries are optimized for performance. Use indexed fields in your WHERE clause, and avoid using negative operators (e.g., NOT, !=) or leading wildcards in LIKE expressions.
  7. Cache Results: If you need to reuse the same data multiple times in a transaction, consider caching the results in a collection so that you don’t need to query the database again.
  8. Use @future or Queueable Apex: If your operation can be split into multiple transactions, consider using asynchronous processing with @future methods or Queueable Apex to spread the queries across different transactions.
  9. Review Your Triggers: If you have multiple triggers on the same object, ensure that they are optimized and not executing unnecessary SOQL queries. Consider consolidating logic into fewer triggers.
  10. Test and Monitor: Use the Salesforce Developer Console and debug logs to monitor the number of SOQL queries executed by your code. This can help you identify areas that need optimization.

By following these best practices, you can reduce the number of SOQL queries in your Salesforce code and avoid hitting the governor limits.


Posted

in

by

Tags:

Comments

Leave a Reply

Open Chat
1
Dear Sir/Madam
How can I help you?