Trigger

Development 🟡 Intermediate
📖 4 min read

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.

Real-World 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 Trigger Matters

An Apex Trigger is a block of code that automatically executes before or after specific DML events — insert, update, delete, merge, upsert, and undelete — on Salesforce records. Triggers are the primary mechanism for implementing complex business logic that cannot be achieved through declarative tools like Flow or Process Builder. They operate at the database level, meaning they fire regardless of whether the record change originates from the UI, API, bulk data loads, or other automation. This makes them the most reliable enforcement point for critical data validation, cross-object updates, and integration callouts.

As orgs accumulate more triggers across objects, proper trigger architecture becomes crucial. The widely adopted best practice is the one-trigger-per-object pattern, where a single trigger delegates to handler classes that contain the actual business logic. Without this pattern, orgs end up with multiple triggers on the same object with unpredictable execution order, making debugging nearly impossible. Poorly written triggers that are not bulkified will fail spectacularly during data migrations or batch operations, and triggers without adequate test coverage become a deployment bottleneck since Salesforce requires at least 75% code coverage for production deployments.

How Organizations Use Trigger

  • QuantumPay Payments — QuantumPay uses a before-insert trigger on the Payment object that queries the related Account's credit limit. If a new payment would push the customer over their limit, the trigger adds an error to the record using addError(), blocking the save. This validation runs consistently whether the payment is entered by a rep in the UI or created via the API by their mobile app.
  • Atlas Supply Chain — Atlas has an after-update trigger on the Shipment object that automatically creates a Case record when a shipment status changes to 'Delayed.' The trigger populates the Case with shipment details, assigns it to the logistics team queue, and sets the priority based on the shipment's SLA tier, reducing manual case creation time from 5 minutes to zero.
  • EverGreen Energy — EverGreen's after-insert trigger on the Meter Reading object calculates the energy consumption delta between the current and previous reading, then updates the customer's Account record with the latest usage data. This trigger processes thousands of readings uploaded daily via Data Loader, handling bulk operations efficiently through proper bulkification.

🧠 Test Your Knowledge

See something that could be improved?

Suggest an Edit