Too Many SOQL Queries: 101 in Salesforce

The System.LimitException: Too many SOQL queries: 101 is the most critical “stop-work” order in the Salesforce ecosystem. It occurs the exact moment your Apex code or automation attempts to execute its 101st SOQL query within a single transaction. Because Salesforce operates on a multi-tenant architecture, these strict “Governor Limits” are enforced to prevent a single poorly optimized process from monopolizing shared database resources and slowing down the entire cloud for other users.

Why This Error is a Critical Priority

When you hit the 101 limit, there is no “grace period” or partial success. The entire transaction fails instantly. Any data changes made during that execution are rolled back, users are blocked from saving records, and integrated systems may begin to desync.

In modern Salesforce environments—packed with complex Record-Triggered Flows, Apex Triggers, and Managed Packages – this error is the #1 cause of deployment failures and production outages. If your org is scaling, “Error 101” isn’t just a possibility; it is an inevitable roadblock that requires immediate architectural intervention before it compromises your data integrity.

Common Error Message

System.LimitException: Too many SOQL queries: 101

You may see this during:
  • Record saves
  • Trigger execution
  • Batch or Queueable Apex
  • Data loads
  • Unit test execution
Why This Error Happens (Most Common Causes)
  1. SOQL Queries Inside Loops

    The number one cause.

    Bad Example

    for (Account acc : Trigger.new) {
    					Contact c = [SELECT Id FROM Contact WHERE AccountId = :acc.Id];
    				}

    Each loop iteration runs a new query, quickly hitting the limit.

  2. Multiple Triggers on the Same Object

    When several triggers (or poorly structured trigger frameworks) fire together, queries stack up fast.

  3. Flow + Apex + Process Builder Running Together

    Even if your Apex looks clean, combined automation can silently push you over the limit.

  4. Recursive Trigger Execution

    Triggers that update records on the same object can re-fire and re-run SOQL queries unintentionally.

  5. Inefficient Helper Classes

    Utility or helper methods that run queries without checking context often cause hidden limit issues.

How to Fix “Too Many SOQL Queries: 101”

  • ✅ Move Queries Outside Loops

    Always query once and store results in collections.

    Good Example

    Set accIds = new Set();
    			for (Account acc : Trigger.new) {
    				accIds.add(acc.Id);
    			}
    Map<Id, Contact> contactsByAccount = new Map<Id, Contact>(
    				[SELECT Id, AccountId FROM Contact WHERE AccountId IN :accIds]
    			);
  • ✅ Use Maps for Fast Lookups

    Maps prevent repeated queries and improve performance.

  • ✅ Consolidate Automation

    Review:

    • Duplicate flows
    • Old Process Builder automation
    • Unused triggers

    Fewer automations = fewer queries.

  • ✅ Add Recursion Control

    Use static variables or framework-level checks to prevent repeated execution.

  • ✅ Optimize Test Classes

    Poor test data setup often causes unnecessary query execution during tests.

How to Identify the Problem Quickly
  1. Enable Debug Logs
  2. Look for repeated SOQL entries
  3. Check query count after each trigger execution
  4. Review automation order on the object
Best Practices to Prevent This Error
  • Never place SOQL inside loops
  • Use bulk-safe Apex patterns
  • Query only required fields
  • Avoid querying the same object multiple times
  • Review automation impact before deployments
Who Should Handle This Error?
Primary Role
  • 👨‍💻 Salesforce Developer
Secondary Role
  • 🧠 Salesforce Architect (for large-scale automation design)

Admins usually cannot fix this without code changes.

Quick Developer Checklist
  • ✔ Remove SOQL from loops
  • ✔ Combine queries
  • ✔ Review automation stack
  • ✔ Test with bulk data

Get the latest tips, news, updates, advice, inspiration, and more….

Need help fixing this in your org?

Our Salesofrce experts can debug and resolve this issue quickly.