Monday, November 23, 2026
|

TECH & VENTURE

Technology Explained. Ventures Decoded.

Salesforce•Salesforce Development

Salesforce Apex Triggers Explained with Practical Examples

Mastering the Trigger Handler Pattern, Context Routing, Bulkification Mechanics, and Recursion Defense in Enterprise Salesforce Architectures.

By Marcus Vance • London Bureau
Nov 20, 2026• 12 min read
Salesforce Apex Triggers Explained with Practical Examples

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 VariableData TypeAvailable InPrimary Architectural Use Case
Trigger.isBeforeBooleanAll EventsValidation, field defaulting, pre-save business computations without extra DML.
Trigger.isAfterBooleanAll EventsOperating on related objects using the system-generated Record Id.
Trigger.newList<sObject>Insert, Update, UndeleteThe incoming list of records being evaluated in the current chunk.
Trigger.newMapMap<Id, sObject>Insert (After only), Update, DeleteInstant key-value lookup of records by their unique 18-character Salesforce Id.
Trigger.oldMapMap<Id, sObject>Update, DeleteComparing 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

AccountTrigger.triggerapex
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

AccountTriggerHandler.clsapex
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

SalesforceApexTriggersSoftware ArchitectureGovernor Limits

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.

Further Reading • Salesforce Desk

Full Desk Archive →
Agentforce Explained: Features, Architecture and Use Cases
Salesforce AI & Agentforce•14 min read

Agentforce Explained: Features, Architecture and Use Cases

Agentforce represents Salesforce's shift from conversational copilots to truly autonomous enterprise AI agents. Powered by the Atlas Reasoning Engine and anchored by Data Cloud, Agentforce executes multi-step business tasks without constant human prompting.

By Kenji Sato
Lightning Web Components: A Complete Beginner's Guide
Salesforce Development•10 min read

Lightning Web Components: A Complete Beginner's Guide

LWC fundamentally transformed Salesforce frontend engineering by embracing standard Web Components, Shadow DOM encapsulation, and ECMAScript 2026 standards. Understand decorators, the Wire Service, and component communication.

By Elena Rostova
SOQL vs SOSL: Key Differences and Practical Examples
Salesforce Development•9 min read

SOQL vs SOSL: Key Differences and Practical Examples

Both SOQL and SOSL are essential tools for extracting data from Salesforce, but they rely on fundamentally different database engines, indexing strategies, and governor limit allocations.

By Marcus Vance
The Executive Dispatch

Independent Analysis on Software Systems & Venture Capital

Published twice weekly from our bureaus in San Francisco, London, Tokyo, and New York. No sponsored promotions, marketing hype, or unvetted press releases.

Zero spam. Delivered every Tuesday & Friday. Unsubscribe at any time.