Scalable Trigger Architecture for Enterprise Salesforce Orgs
A well-architected Apex trigger framework ensures 100% bulkification, prevents trigger recursion, enforces single-responsibility handlers, and bypasses governor limit bottlenecks.
// Production Apex Trigger Handler Pattern
public with sharing class AccountTriggerHandler implements ITriggerHandler {
private static Set<Id> processedIds = new Set<Id>();
public void beforeInsert(List<Account> newRecords) {
// Enforce OWD & Record Data Cleanliness
AccountService.standardizeTaxIds(newRecords);
}
public void afterUpdate(List<Account> newRecords, Map<Id, Account> oldMap) {
List<Account> recordsToProcess = new List<Account>();
for (Account acc : newRecords) {
// Prevent Recursion using static set verification
if (!processedIds.contains(acc.Id) && acc.AnnualRevenue != oldMap.get(acc.Id).AnnualRevenue) {
processedIds.add(acc.Id);
recordsToProcess.add(acc);
}
}
if (!recordsToProcess.isEmpty()) {
System.enqueueJob(new SyncRevenueQueueable(recordsToProcess));
}
}
}