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

If you’ve been working with Salesforce Apex for any amount of time, you’ve almost certainly run into this error at least once:

System.LimitException: Too many SOQL queries: 101

In this tutorial, I’ll 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 org’s runaway code doesn’t 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.

Too Many SOQL Queries in Salesforce Apex

The moment your code tries to fire query number 101, Salesforce throws System.LimitException: Too many SOQL queries (101), so the entire transaction rolls back. It doesn’t warn you. It just stops.

This isn’t a soft warning you can ask Salesforce Support to increase. It’s a hard stop. There’s no way around the limit itself — the only fix is writing better code.

Why Does System.LimitException Usually Happens in Apex?

In my experience, this error almost always comes down to one of three root causes:

  • SOQL queries inside a for loop — the most common culprit by far
  • Recursive triggers — a trigger calls itself repeatedly, running queries on every cycle
  • Too many automations stacking up — Flows, Process Builders, and Apex all running in the same transaction, each with its own queries

Let me go through each one with real code examples.