Apex Aide apexaide

System.LimitException: Too Many SOQL Queries — What It Means and How to Fix It

Salesforce FAQs· ·Intermediate ·Developer ·13 min read
Summary

The article explains the root causes and fixes for the common Salesforce error System.LimitException: Too many SOQL queries, which occurs when code exceeds the 100-query limit in synchronous Apex transactions. It highlights typical mistakes like querying inside loops, recursive triggers, and stacking automations, providing practical bulkification techniques such as using Maps, moving DML operations outside loops, and applying static boolean guards for recursion. It also covers how Flows can contribute to SOQL limits and offers debugging steps using debug logs and Limits methods. With clear examples, this guide is essential for Salesforce developers and admins aiming to write efficient, scalable Apex code and avoid platform governor limits.

Takeaways
  • Never run SOQL queries or DML inside loops; bulkify using collections and Maps.
  • Use static Boolean guards in triggers to prevent recursive execution.
  • In Flows, fetch all records upfront and filter in-memory to avoid query limits.
  • Use debug logs and Limits.getQueries() to identify where queries are fired.
  • Write bulk-safe code by testing with 200+ records to avoid hitting governor limits.

If youye been working with Salesforce Apex for any amount of time, youe almost certainly run into this error at least once: System.LimitException: Too many SOQL queries: 101 In this tutorial, Ill walk you through what this error means, why Salesforce enforces it, what causes it in real code, and  most importantly  how to fix the error: System.LimitException: Too Many SOQL Queries , with practical examples you can use right away. What Is This System.LimitException: Too Many SOQL Queries Error, Exactly? Salesforce runs on a multi-tenant architecture. That means thousands of companies share the same infrastructure. To make sure one orgs runaway code doesnt choke the entire platform, Salesforce enforces something called Governor Limits . One of those limits is: You can only run 100 SOQL queries in a single synchronous transaction. For asynchronous (like Batch Apex), that limit goes up to 200. The moment your code tries to fire query number 101, Salesforce throws System.

ApexPerformance & LimitsFlow BuilderSalesforce Errors and SolutionsSOQL Exception in Apex