Salesforce Apex Triggers Explained with Practical Examples
Mastering the Trigger Handler Pattern, Context Routing, Bulkification Mechanics, and Recursion Defense in Enterprise Salesforce Architectures.
Figure 2: Architectural lifecycle of an enterprise Apex Trigger execution showing context routing and governor limits.
Key Findings & Architecture Notes
- •Enforce a strict One-Trigger-Per-Object architecture to ensure deterministic execution ordering across all DML operations.
- •Delegate all business logic immediately out of the .trigger file into a dedicated TriggerHandler class and domain service layer.
- •Always bulkify: process records in iterative sets and never execute SOQL queries or DML statements inside loops.
- •Prevent accidental recursion using static boolean flags or Set<Id> processed registries rather than ad-hoc counters.
- •Respect the 100 SOQL query and 150 DML statement synchronous transactional limits by accumulating records into bulk collections.
Apex Trigger Context Variables Matrix
| Context Variable | Data Type | Available In | Primary Architectural Use Case |
|---|---|---|---|
| Trigger.isBefore | Boolean | All Events | Validation, field defaulting, pre-save business computations without extra DML. |
| Trigger.isAfter | Boolean | All Events | Operating on related objects using the system-generated Record Id. |
| Trigger.new | List<sObject> | Insert, Update, Undelete | The incoming list of records being evaluated in the current chunk. |
| Trigger.newMap | Map<Id, sObject> | Insert (After only), Update, Delete | Instant key-value lookup of records by their unique 18-character Salesforce Id. |
| Trigger.oldMap | Map<Id, sObject> | Update, Delete | Comparing previous state against new state to detect specific field modifications. |
The One-Trigger-Per-Object Design Principle
In enterprise Salesforce implementations, an unmanaged trigger is an invitation to production instability. When multiple developers write separate triggers on the same object, Salesforce provides zero guarantee regarding the sequence of execution. Modern engineering teams enforce a single dispatcher trigger per object.
Production Code: AccountTrigger.trigger
trigger AccountTrigger on Account (
before insert, after insert,
before update, after update,
before delete, after delete,
after undelete
) {
AccountTriggerHandler handler = new AccountTriggerHandler();
handler.run();
}The Domain Handler: AccountTriggerHandler.cls
public with sharing class AccountTriggerHandler {
private static Set<Id> processedAccountIds = new Set<Id>();
public void run() {
if (Trigger.isBefore && Trigger.isInsert) {
handleBeforeInsert((List<Account>) Trigger.new);
} else if (Trigger.isAfter && Trigger.isInsert) {
handleAfterInsert((Map<Id, Account>) Trigger.newMap);
}
}
private void handleBeforeInsert(List<Account> newAccounts) {
for (Account acc : newAccounts) {
if (acc.BillingCountry != null) {
acc.BillingCountry = acc.BillingCountry.toUpperCase();
}
}
}
private void handleAfterInsert(Map<Id, Account> newAccountMap) {
List<Task> onboardingTasks = new List<Task>();
for (Account acc : newAccountMap.values()) {
if (acc.Type == 'Customer - Direct') {
onboardingTasks.add(new Task(
Subject = 'Executive Onboarding: ' + acc.Name,
WhatId = acc.Id,
Priority = 'High',
Status = 'Not Started'
));
}
}
if (!onboardingTasks.isEmpty()) {
insert onboardingTasks;
}
}
}Subject Index
About Marcus Vance
Principal Salesforce Technical Architect & Venture Contributor • London Bureau
Marcus Vance is a certified Salesforce Technical Architect and systems engineer with over 12 years of experience designing multi-cloud enterprise topologies, high-volume Apex integrations, and CRM infrastructure across European banking and SaaS enterprises.