Before Trigger
Learning Content
Last updated: July 21, 2026
Before Trigger
A Before Trigger is an Apex Trigger that executes before a record is saved to the Salesforce database. It allows developers to modify field values, perform complex business validations, and execute custom logic before the record is committed.
Unlike a Before-Save Flow, a Before Trigger is written using Apex and is designed for scenarios that require advanced programming logic, reusable methods, loops, collections, custom classes, or complex business rules.
💡 LearnFrenzy Insight
Imagine an airport security officer inspecting your luggage.
If something doesn't meet the rules, you're stopped before boarding.
Similarly, a Before Trigger checks and updates records before Salesforce permanently saves them.
Where Does Before Trigger Execute?
User Clicks Save
│
▼
System Validation
│
▼
Before-Save Flow
│
▼
⭐ Before Trigger
│
▼
Validation Rules
│
▼
Duplicate Rules
│
▼
Database Save
Before Triggers execute after the Before-Save Flow (when applicable) and before the record is written to the database, making them ideal for preparing or validating data before persistence.
Why Use a Before Trigger?
| Capability | Description |
|---|---|
| Complex Business Logic | Implement advanced validation and processing using Apex. |
| Field Updates | Modify values on the same record before it is saved. |
| Reusable Code | Call helper classes, utility methods, and trigger frameworks. |
| Bulk Processing | Handle multiple records efficiently using collections. |
| Advanced Validation | Apply custom business rules that go beyond Validation Rules. |
Common Use Cases
| Business Requirement | Before Trigger Solution |
|---|---|
| Generate a custom field value | Populate fields before saving. |
| Complex eligibility checks | Validate against multiple business conditions. |
| Cross-record validation | Query related records and enforce business rules. |
| Prevent invalid updates | Use addError() to stop the transaction. |
| Apply reusable business logic | Invoke Apex service classes or trigger handlers. |
Simple Apex Example
trigger AccountBeforeTrigger on Account (before insert, before update) {
for(Account acc : Trigger.new){
if(acc.Rating == null){
acc.Rating = 'Warm';
}
}
}
In this example, if the Rating field is empty, the trigger assigns a default value before Salesforce saves the record.
Real Business Scenario
🏢 Business Example
A financial institution allows loan applications only if:
- The applicant is at least 21 years old.
- The annual income exceeds the minimum requirement.
- The customer does not already have an active loan of the same type.
These checks involve multiple business rules and related records, making a Before Trigger a better solution than a simple Flow.
Before Trigger vs Before-Save Flow
| Feature | Before Trigger | Before-Save Flow |
|---|---|---|
| Programming Required | Yes (Apex) | No |
| Complex Logic | Excellent | Limited |
| Reusable Methods | Yes | No |
| Trigger Framework Support | Yes | No |
| Maintainable by Admins | No | Yes |
| Best For | Advanced business logic | Simple same-record field updates |
When Should You Use a Before Trigger?
| Use Before Trigger | Avoid Before Trigger |
|---|---|
| Complex calculations | Simple default field values |
| Reusable Apex logic | Basic formatting logic |
| Cross-record validation | Simple declarative automation |
| Advanced business rules | Tasks easily handled by Before-Save Flow |
🔍 Behind the Scenes: What Happens Internally?
User Clicks Save
│
▼
Before-Save Flow Executes
│
▼
Before Trigger Starts
│
▼
Apex Runtime Executes Logic
│
▼
Trigger.new Records Updated
│
▼
Validation Rules Execute
│
▼
Database Save Happens Later
A Before Trigger works with the in-memory version of the records stored in Trigger.new. Changes made to these records become part of the initial save operation, eliminating the need for an additional update on the same record.
✅ Best Practice
Keep your trigger lightweight. Move business logic into a dedicated Trigger Handler or service class, bulkify your code, and avoid SOQL or DML operations inside loops.
⚠️ Common Mistake
Using a Before Trigger for simple field updates that can be handled with a Before-Save Flow. Prefer declarative automation whenever it satisfies the requirement, and reserve Apex for complex business logic.
🎯 Interview Tip
Question: Why would you choose a Before Trigger instead of a Before-Save Flow?
Answer: Choose a Before Trigger when the solution requires complex Apex logic, reusable classes, advanced validations, cross-record processing, or business rules that cannot be implemented efficiently with declarative automation.
📌 Quick Revision
- ✔ Executes before the record is saved.
- ✔ Runs after Before-Save Flow.
- ✔ Can modify values in
Trigger.new. - ✔ Ideal for complex business logic and validations.
- ✔ Supports reusable Apex classes and trigger frameworks.
- ✔ No additional DML is required to update the same record.
➡ Next Lesson
In the next lesson, you'll learn how Validation Rules execute during the Salesforce Order of Execution, how they differ from Apex validations, and when to use each approach.
Practice What You've Learned
Test your understanding with these practice exercises