Definition
Trigger Context Variable is a Salesforce development feature that provides developers with the ability to create custom solutions on the Lightning Platform. It supports building robust, scalable applications that integrate with Salesforce's data and security model.
Real-World Example
At their company, a Salesforce developer at CodeBridge leverages Trigger Context Variable to create a robust integration between Salesforce and an external system. Using Trigger Context Variable, the developer builds an efficient solution that syncs data in near real-time, handles error scenarios gracefully, and includes detailed logging for troubleshooting.
Why Trigger Context Variable Matters
Trigger Context Variables are system-defined variables available inside Apex triggers that provide critical information about the current trigger execution. Variables like Trigger.new, Trigger.old, Trigger.newMap, and Trigger.oldMap give developers access to the records being processed, both in their current and previous states. Additional boolean variables such as Trigger.isBefore, Trigger.isAfter, Trigger.isInsert, Trigger.isUpdate, and Trigger.isDelete let the code determine exactly which DML operation and timing context triggered the execution. These variables are the foundation for writing context-aware trigger logic that behaves correctly across all scenarios.
As trigger logic grows in complexity, proper use of context variables separates functional code from fragile code. Comparing Trigger.old and Trigger.new field values is essential for detecting actual changes — without this comparison, trigger logic fires on every save even when the relevant field was not modified, causing unnecessary processing and potential recursion. The Trigger.operationType variable (available in newer API versions) provides a cleaner alternative to multiple boolean checks. Developers who do not leverage Trigger.newMap and Trigger.oldMap for efficient record lookups end up writing nested loops that degrade performance when processing large record sets, making context variables both a correctness and a performance concern.
How Organizations Use Trigger Context Variable
- Quantum Dynamics — Quantum Dynamics uses Trigger.oldMap and Trigger.newMap in their Opportunity trigger to detect when the Stage field changes from any value to 'Closed Won.' By comparing old and new values, the trigger only fires the commission calculation logic when the stage actually changes, not on every Opportunity save, reducing unnecessary processing by 85%.
- BrightStar Medical — BrightStar's Contact trigger uses Trigger.isBefore and Trigger.isInsert to normalize phone number formats before records are saved. By checking the context variables, the same trigger handler also uses Trigger.isUpdate with Trigger.old comparisons to only re-normalize when the phone field actually changes, preventing unnecessary field updates on unrelated edits.
- Cobalt Logistics — Cobalt uses Trigger.isDelete and Trigger.old in their Shipment trigger to archive deleted shipment records into a custom History object before they are permanently removed. The trigger accesses the complete record data through Trigger.old since Trigger.new is not available in delete context, ensuring no shipment data is lost even when records are deleted.