Trigger
An Apex Trigger is a piece of Apex code that executes before or after data manipulation language (DML) events on Salesforce records, such as insert, update, delete, merge, upsert, and undelete.
Definition
An Apex Trigger is a piece of Apex code that executes before or after data manipulation language (DML) events on Salesforce records, such as insert, update, delete, merge, upsert, and undelete. Triggers allow developers to perform custom actions when records are modified, such as validating data, updating related records, or calling external services.
In plain English
“Imagine this: You know how LEGO bricks snap together to build cool things? Trigger is like a special kind of LEGO brick that Salesforce developers use. They write instructions (called code) to build custom features that don't come in the basic Salesforce box - kind of like building your own unique LEGO creation instead of following the instructions on the box.”
Worked example
A developer at QuantumPay writes a before-insert trigger on the Payment object. The trigger checks whether the payment amount exceeds the customer's credit limit by querying the related Account record. If the limit would be exceeded, the trigger adds an error to the record, preventing the save. This business logic runs every time a payment is created, whether from the UI, API, or Data Loader.
Why a Trigger is the code that runs automatically when records change
An Apex Trigger is code attached to a specific object that executes automatically when records on that object are inserted, updated, deleted, undeleted, merged, or upserted. Triggers run before or after the DML event - before to validate or transform data before the platform commits the change, after to react to the change with downstream actions. For developers, triggers are how custom business logic gets enforced consistently across every code path that touches a record.
The reason triggers earn dedicated patterns and best practices is that they run for every operation, with full power and full danger. A trigger that queries inside a loop crashes on bulk loads; a trigger that fires another trigger creates recursion; multiple triggers on the same object run in non-deterministic order and step on each other. Modern Salesforce dev follows the Trigger Handler pattern (one trigger per object delegating to a class), bulkifies every operation, and tests at scale before production deployment.
How organizations use Trigger
Use a before-update Trigger on Account to enforce that any change to the credit-rating field cascades into refreshed credit limits on open Opportunities. The cascade is real-time; declarative-only solutions cannot enforce the calculation order this Trigger does.
Implement an after-insert Trigger on a custom Inspection__c object that validates GIS coordinates against a parcel-boundary library and creates Tasks for any inspection outside its expected boundary. The validation requires Apex; declarative formulas cannot evaluate point-in-polygon.
Replaced their last legacy Workflow Rule with an Apex Trigger that handles compound conditions across three objects (Account, Listing, Showing) - the Workflow Rule's single-object scope had been the constraint that finally forced the refactor.
Trust & references
Straight from the source - Salesforce's reference material on Trigger.
- Apex TriggersSalesforce Developers
- Triggers OverviewSalesforce Developers
About the Author
Dipojjal Chakrabarti is a B2C Solution Architect with 29 Salesforce certifications and over 13 years in the Salesforce ecosystem. He runs salesforcedictionary.com to help admins, developers, architects, and cert/interview candidates sharpen their fundamentals. More about Dipojjal.
Test your knowledge
Q1. What is a Governor Limit in the context of Trigger?
Q2. Where would a developer typically work with Trigger?
Q3. What is required before deploying Trigger-related code to production?
Discussion
Loading discussion…