10 things you can do to avoid SOQL 101 errors in Salesforce

Dealing with SOQL 101 errors in Salesforce can be frustrating, but with some smart practices, you can avoid running into this problem. Here’s 10 things you can do to avoid SOQL 101 errors:

1. Bulkify Your Code

Think big! Always write your code to handle multiple records at once instead of one at a time. This means you’ll avoid running a query every single time you process a record.

2. Keep SOQL Out of Loops

One of the easiest ways to run into the SOQL 101 limit is to have queries inside loops. Instead, gather your record IDs in a collection, then run your query once outside the loop.

// Bad way
for (Contact c : contactList) {
    Account acc = [SELECT Id, Name FROM Account WHERE Id = :c.AccountId];
}

// Better way
Set<Id> accountIds = new Set<Id>();
for (Contact c : contactList) {
    accountIds.add(c.AccountId);
}
List<Account> accounts = [SELECT Id, Name FROM Account WHERE Id IN :accountIds];
Map<Id, Account> accountMap = new Map<Id, Account>(accounts);
for (Contact c : contactList) {
    Account acc = accountMap.get(c.AccountId);
}

3. Selective Queries

Only ask for what you need. Instead of SELECT *, specify just the fields you actually need in your query. It’s faster and more efficient.

// Not so good
List<Account> accounts = [SELECT * FROM Account];

// Much better
List<Account> accounts = [SELECT Id, Name, BillingCity FROM Account];

4. Use Filters

Use WHERE clauses to limit the records you pull. This way, you’re not fetching unnecessary data.

List<Account> accounts = [SELECT Id, Name FROM Account WHERE BillingState = 'CA'];

5. Related Records in One Go

You can get related records in a single query using subqueries or relationship queries. It’s like getting everything in one trip.

List<Account> accounts = [SELECT Id, Name, (SELECT Id, LastName FROM Contacts) FROM Account];

6. Know Your Limits

Be aware of Salesforce governor limits. Check how many queries you’ve used and how many you have left with Limits.getQueries() and Limits.getLimitQueries().

7. Use Asynchronous Processing

For tasks that don’t need to be immediate, like big data operations, use Batch Apex or future methods. This spreads out your workload.

@future
public static void doAsyncProcessing() {
    // Your logic here
}

8. Smart Triggers

Use a trigger handler pattern to manage your logic. This makes sure your triggers are bulkified and efficient.

trigger AccountTrigger on Account (before insert, before update) {
    if (Trigger.isBefore) {
        if (Trigger.isInsert) {
            AccountTriggerHandler.beforeInsert(Trigger.new);
        } else if (Trigger.isUpdate) {
            AccountTriggerHandler.beforeUpdate(Trigger.new, Trigger.oldMap);
        }
    }
}

9. Caching is Your Friend

Use custom settings or custom metadata types to cache frequently accessed data. This reduces the number of queries you need to run.

10. Plan Your Queries

Sometimes a little planning goes a long way. Think about what data you need and when, so you can run queries efficiently without hitting limits.

Following these tips will help you avoid those pesky SOQL 101 errors and keep your Salesforce org running smoothly. Happy coding!

Leave a Comment