Software Development

What is the best way to handle "Too Many SOQL Queries: 101" in a complex Trigger framework?

DA Asked by David Thompson · 22-11-2025
0 upvotes 18,452 views 0 comments
The question

Our org is hitting the 101 SOQL limit during bulk uploads. We use a Trigger Handler pattern, but with multiple managed packages installed, the total query count is exploding. I've bulkified my own code, but the cumulative limit is still a problem. Does anyone have advice on using "Static Sets" to prevent redundant queries or implementing a "Service Layer" to manage data access across the entire transaction?

3 answers

0
PA
Answered on 14-02-2025

To combat the 101 limit in complex environments, you need a "Singleton" pattern for your data access. By creating a static Map or Set within a service class, you can cache query results for the duration of the transaction. Before running any SOQL, your code should check if the data already exists in the static variable. Additionally, check your "Order of Execution." If you have multiple workflows or flows triggering the same Apex multiple times, your query count will double or triple. Consolidating logic into a single Trigger per object is the industry standard for keeping the query footprint small.

0
MI
Answered on 10-03-2025

Have you analyzed the "Query Plan Tool" in the Developer Console? Sometimes the issue isn't the number of queries, but that the queries are non-selective, causing the database to work harder and stay open longer. Could adding indexed filters to your SOQL help reduce the overall transaction time and potential for hitting limits during heavy processing?

DA 15-03-2025

Michael, the Query Plan Tool showed that my queries were selective, but the sheer frequency was the killer. I implemented Patricia's Singleton suggestion by caching the RecordType IDs and Profile data in a static utility class. This alone reduced my total SOQL count by 30 queries per transaction! It’s amazing how much overhead those repetitive "lookups" add when you’re dealing with 200-record batches.

0
SU
Answered on 20-04-2025

Also, consider moving non-critical logic to @future or Queueable Apex. It resets the governor limits for that specific execution, giving you a fresh 100 SOQL queries to work with.

PA 25-04-2025

Exactly, Susan. Queueable is especially powerful because you can chain jobs and even pass complex objects, which makes it much more flexible than @future for asynchronous heavy lifting.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

World globe icon Country: Canada

Book Free Session