Event-Driven Programming
Learning Content
Last updated: July 21, 2026
Event-Driven Programming in Apex
One of the most powerful features of Salesforce Apex is its Event-Driven Programming model. Unlike traditional applications where code runs only when a user explicitly executes it, Apex automatically responds to business events occurring within the Salesforce platform.
An event is any action performed by a user, another application, or the Salesforce platform itself. Whenever an event occurs, Apex can automatically execute the appropriate business logic without requiring manual intervention.
💡 LearnFrenzy Insight
Think about an automatic sliding door at a shopping mall.
Nobody presses a button to open it.
The moment someone approaches the entrance, the sensor detects the event and automatically opens the door.
Salesforce Apex behaves in a very similar way.
When a business event occurs, Apex automatically executes the required logic.
What is an Event?
An event is any action that occurs within Salesforce and requires the system to perform additional processing automatically.
| Business Event | Automatic Apex Action |
|---|---|
| New Account Created | Create default Contact records |
| Opportunity Closed Won | Create an Order and send an email |
| Case Updated | Notify the Support Manager |
| Lead Converted | Create Account and Contact records |
| Product Price Changed | Recalculate related quotations |
How Event-Driven Programming Works
Whenever an event occurs, Salesforce automatically invokes the appropriate Apex logic to process that event.
User Saves Record
│
▼
Salesforce Detects Event
│
▼
Trigger Executes
│
▼
Business Logic Runs
│
▼
Database Updated
Developers do not need to manually execute the code. Salesforce automatically detects the event and executes the associated Apex logic.
Simple Trigger Example
The following Trigger automatically executes whenever a new Account record is created.
trigger AccountTrigger
on Account(before insert){
for(Account acc : Trigger.new){
acc.Description =
'Created using Apex Trigger';
}
}
What Happens?
- A user creates a new Account.
- The before insert event occurs.
- Salesforce automatically executes the Trigger.
- The Description field is populated before the record is saved.
Real Business Scenario
🏢 Business Example
Suppose a sales representative changes an Opportunity Stage to Closed Won.
Instead of asking the user to perform multiple manual steps, Salesforce automatically triggers Apex code that:
- Creates an Order.
- Generates an Invoice.
- Sends a confirmation email.
- Notifies the Finance Team.
- Updates inventory information.
Everything happens automatically in response to a single business event.
Common Salesforce Events
| Trigger Event | Description |
|---|---|
| before insert | Runs before a record is created. |
| after insert | Runs after a record is created. |
| before update | Runs before a record is updated. |
| after update | Runs after a record is updated. |
| before delete | Runs before a record is deleted. |
| after delete | Runs after a record is deleted. |
| after undelete | Runs when a deleted record is restored. |
Where is Event-Driven Programming Used?
| Feature | Business Purpose |
|---|---|
| Triggers | Execute business logic automatically |
| Platform Events | Event-based communication |
| Change Data Capture | Track record changes |
| Flows | Automation without code |
| Batch Processing | Process data after specific events |
✅ Best Practice
Keep Trigger logic lightweight. Place complex business logic inside Apex classes instead of writing everything directly inside the Trigger. This improves readability, maintainability, and testability.
⚠️ Common Mistake
Avoid placing SOQL queries or DML operations inside loops within Triggers. This can quickly exceed Salesforce Governor Limits and negatively impact application performance.
🎯 Interview Tip
Question: Why is Apex called an event-driven programming language?
Answer: Apex automatically executes business logic in response to Salesforce events such as record creation, updates, deletion, platform events, and other system actions. Developers define the logic, while Salesforce determines when it should run.
📚 Learn More
This section introduces Event-Driven Programming as one of Apex's core features.
In the dedicated Triggers chapter, you'll learn:
- Before and After Triggers
- Trigger Context Variables
- Trigger Framework Design
- Bulkification Techniques
- Recursion Prevention
- Order of Execution
- 50+ Practical Trigger Examples
Practice What You've Learned
Test your understanding with these practice exercises