Platform
Too many SOQL queries: 101 in Apex
Find repeated queries across triggers and automation, collect IDs and query once, then test the full transaction at bulk volume.
A query hidden inside a helper still runs inside the caller’s loop. Start by tracing how often the helper executes.
The governor reference lists 100 ordinary synchronous SOQL queries and 200 for common asynchronous execution. Scheduled Apex follows synchronous limits. Certified managed-package namespaces have specific allowances, so inspect the log’s namespace context rather than assuming one universal counter.
Replace repeated lookups with a map
This illustrative fragment assumes contacts is a populated input collection. It collects distinct Account IDs and retrieves their names in one query.
Set<Id> accountIds = new Set<Id>();
for (Contact contactRecord : contacts) {
if (contactRecord.AccountId != null) {
accountIds.add(contactRecord.AccountId);
}
}
Map<Id, Account> accountsById = new Map<Id, Account>();
if (!accountIds.isEmpty()) {
accountsById = new Map<Id, Account>([
SELECT Id, Name FROM Account WHERE Id IN :accountIds
]);
}
Read from accountsById during subsequent processing, and handle a missing entry explicitly. Production code also needs the appropriate sharing and field-access behavior. A missing record can result from permissions or bad input.
Salesforce’s bulk-trigger guidance explains collection-based processing. Applying it only to the visible trigger is insufficient if another service performs one query per record.
Inspect the full save path
Use Limits.getQueries() around the suspected operation and compare it with Limits.getLimitQueries(). The Limits reference documents both methods. Run the measurement against a collection that activates the relevant branches.
Check record-triggered flows, related-object updates and recursion. Re-querying the same parent in several handlers can consume the budget even when each handler appears efficient in isolation. Consolidate retrieval at a service boundary where the required fields are known.
Caching needs a defined lifetime. A transaction-local cache may be useful, but it must account for records changed during that transaction. A static map that returns stale values can replace a visible exception with incorrect business behavior.
Make the test assert the business result
Test a bulk collection containing shared parents, missing parent references and records that take different paths. Assert that every eligible record was processed. Then inspect the query count for a regression bound appropriate to the operation.
Don’t solve the error by returning after the counter approaches its ceiling unless skipped work is deliberately recorded for later processing. Silent omission is especially difficult to detect in a successful import.
If the workload requires separate transactions, design the asynchronous handoff and failure recovery explicitly. Also check the 50,000-row limit: reducing query count can produce a single query that retrieves too much data.