Earn a Salesforce AI Certification by E&ICTA, IIT Kanpur + 12-Week Internship Secure Your Spot Today!

Earn a Salesforce AI Certification by E&ICTA, IIT Kanpur + 12-Week Internship Secure Your Spot Today!
Too Many SOQL Queries 101 Causes and Fixes Explained

Too Many SOQL Queries 101: Causes and Fixes Explained

When working in Salesforce, the dreaded “Too many SOQL Queries: 101” error is a common roadblock for developers. It signals that the code execution has breached the Salesforce governor limits, which are designed to maintain a balanced, multi-tenant environment. 

But don’t worry—this guide will help you understand the causes and fixes, ensuring your code runs smoothly and efficiently.

What is the “Too Many SOQL Queries: 101” Error?

The “Too many SOQL Queries: 101″ error occurs when more than 100 SOQL queries are executed in a single transaction in synchronous contexts (or 200 in asynchronous). This hard limit is imposed to ensure fair resource distribution and prevent inefficient code from monopolizing resources.

Understanding Salesforce Governor Limits

Salesforce governor limits are like speed limits on a highway. They ensure smooth traffic flow by capping resource usage for every transaction. The 100-SOQL-query limit in synchronous processing is one of the most common limits developers encounter.

Common Causes of SOQL Query Errors

1. SOQL Inside a For Loop

Placing a SOQL query inside a loop triggers a new query for every iteration. If the loop runs multiple times, you quickly hit the 100-query limit.

2. Non-Bulkified Triggers

Triggers that aren’t designed to handle bulk operations may inadvertently run multiple queries for each record, leading to inefficiency.

3. Recursive Triggers

Triggers that call themselves during processing create a loop of execution, often leading to excessive queries.

4. Concurrent Updates

When multiple processes update the same record simultaneously, each may fire additional queries, overwhelming the transaction.

iit course cta

How to Resolve the “Too Many SOQL Queries: 101” Error

1. Avoid SOQL Queries Inside Loops

Never place SOQL queries inside a loop. Instead, query the data in bulk and process it in memory.
Example:

// Inefficient code

for (Account acc : [SELECT Id FROM Account]) {

    List<Contact> contacts = [SELECT Id FROM Contact WHERE AccountId = :acc.Id];

}

// Optimized code

Map<Id, List<Contact>> accountContacts = new Map<Id, List<Contact>>();

for (Contact con : [SELECT Id, AccountId FROM Contact WHERE AccountId IN :accountIds]) {

    if (!accountContacts.containsKey(con.AccountId)) {

        accountContacts.put(con.AccountId, new List<Contact>());

    }

    accountContacts.get(con.AccountId).add(con);

}

2. Bulkify Triggers

Ensure your triggers are bulkified to handle multiple records efficiently.

  • Use collections to hold data instead of firing queries for individual records.
  • Process records in a single transaction whenever possible.

3. Use @Future Methods

Moving logic to an asynchronous method, like @future, provides higher SOQL limits (200 queries per transaction) and can offload processing.
Example:

@future

public static void processRecordsAsync(Set<Id> recordIds) {

    List<Account> accounts = [SELECT Id, Name FROM Account WHERE Id IN :recordIds];

    // Perform logic here

}

4. Combine Queries

Combine multiple queries into one using relationship queries or aggregate functions.
Example:

// Separate queries

Account acc = [SELECT Id FROM Account WHERE Id = :accountId];

List<Contact> contacts = [SELECT Id FROM Contact WHERE AccountId = :accountId];

// Combined query

Account acc = [SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Id = :accountId];

5. Use Maps and Sets

Maps and Sets reduce redundant queries by processing data in memory rather than hitting the database repeatedly.
Example:

Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Id, Name FROM Account WHERE Id IN :accountIds]);

6. Minimize Recursive Triggers

Implement a static variable in Apex to prevent recursive trigger execution.
Example:

public class TriggerHandler {

    public static Boolean isTriggerRunning = false;

}

7. Employ Batch Apex for Large Data

For processing thousands of records, use Batch Apex, which executes logic in smaller, governor-limit-friendly chunks.

Also Read – Salesforce Data Loader Errors: How to Identify and Fix Them

Preventing Future Issues

1. Follow Salesforce Best Practices

Adopt coding practices that align with Salesforce’s guidelines:

  • Always bulkify your code.
  • Use tools like the Developer Console to monitor SOQL queries.

2. Conduct Regular Code Reviews

Regular peer reviews help catch inefficient code early, preventing issues in production.

3. Leverage Salesforce Tools

Utilize tools like Query Plan, Workbench, and Debug Logs to analyze and optimize SOQL queries.

Also Read – Salesforce Object Query Language (SOQL): Comprehensive Guide

Conclusion

The “Too many SOQL Queries: 101” error doesn’t have to be a stumbling block. By understanding its causes and implementing the fixes outlined above, you can write efficient, scalable Apex code. Think of this as tuning a high-performance engine—small adjustments lead to a big difference in performance.

Want to take your skills to the next level? Explore saasguru’s hands-on Salesforce learning platform, where 30+ certification courses, 50+ mock exams, and real-world labs await. It’s not just about studying—it’s about thinking, practicing, and mastering Salesforce like never before.  

FAQs

1. What is the SOQL query limit in Salesforce?

Salesforce allows up to 100 SOQL queries per transaction in synchronous processes and 200 in asynchronous ones.

2. How can I identify excessive SOQL queries in my code?

Use the Salesforce Developer Console and Apex Debug Logs to track and analyze SOQL query usage.

3. What is bulkification in Salesforce?

Bulkification is the practice of designing code to handle multiple records in a single transaction to optimize resource usage.

4. How can I prevent recursive triggers?

Use a static Boolean variable to track and prevent recursive trigger execution.

5. Why is Salesforce imposing governor limits?

Governor limits ensure fair resource distribution in Salesforce’s multi-tenant environment, maintaining platform stability.

Table of Contents

Subscribe & Get Closer to Your Salesforce Dream Career!

Get tips from accomplished Salesforce professionals delivered directly to your inbox.

Looking for Career Upgrade?

Book a free counselling session with our Course Advisor.

By providing your contact details, you agree to our Terms of use & Privacy Policy

Unlock Your AI -Powered Assistant

Gain Exclusive Access to Your Salesforce Copilot

Related Articles

Too Many SOQL Queries 101: Causes and Fixes Explained

Learn how to resolve the Salesforce ‘Too Many SOQL Queries: 101’ error with causes, fixes, and best practices for optimized Apex code. Read now!

Salesforce Project Manager Salary Trends for 2025

Discover Salesforce Project Manager salary trends for 2025, factors influencing pay, top industries, and ways to boost earning potential.

Humans of Salesforce – Ayush Tiwari

Discover Ayush Tiwari’s journey in Salesforce—his proud moments, AI insights, and top tips for new trailblazers aiming to thrive. Read now!