RP.
Event Monitoring: Active

Automation Hub

The Ignition Point

Apex Triggers are the "trigger points" of Salesforce. They execute custom actions before or after changes occur to your records—whether it's an insertion, update, or deletion.

Full Lifecycle Control

The Bulk Default

"Every trigger is a bulk trigger by default."

They are capable of handling multiple records simultaneously. Always write your logic with Collection Patterns to ensure governance limit compliance.

Strategic Architecture

Why Code Wins

Event Mastery

  • Before Events: Handle logic before records hit the database, unlike most declarative tools that focus on "After" events.
  • Full Lifecycle: Manage Delete and Undelete events that are often restricted in standard automation.

Precision Errors

  • Custom Exceptions: Show users specific, helpful error messages instead of the generic "An error occurred" system faults.
  • Data Validation: Implement advanced checks to stop "bad data" from entering the system at the code level.

Efficiency

  • Partial Success: Unlike the "All or None" framework of Flows, Triggers allow for partial record success during bulk operations.
  • Complex Logic: Manage intricate business rules that would be impossible or unmaintainable in a visual Flow builder.

Execution Context

Trigger Types

Before Triggers

Used primarily to **update additional values** on a record or **validate them** before they are committed to the database. These are the front-line guards of your data integrity.

Action: Pre-Processing
After Triggers

Designed to update **related or unrelated records** and perform post-processing tasks once the initial record has been safely saved to the database.

Action: Post-Processing

Code Structure

The Anatomy

trigger TriggerName on ObjectName (trigger_events) {

// Your custom code block goes here

}

Safety Protocol: Database changes are automatically rolled back if the trigger fails.

Precision Timing

The Seven Events

Pre-Save context
  • Before Insert
  • Before Update
  • Before Delete
Post-Save context
  • After Insert
  • After Update
  • After Delete
Restoration logic
After Undelete

Available only for specific system objects. Invoked when records are recovered from the Recycle Bin.

System.Trigger Class

Context Variables

Operation

isInsert

Collection

New

Timing

isBefore

Operation

isUpdate

Collection

Old

Timing

isAfter

Operation

isDelete

Metadata

Size

Operation

isUndelete

Variables originate from the System.Trigger Class

Guardrails & Constraints

Technical Considerations

DML Zero-Touch

"You cannot use DML operations on Trigger.new or Trigger.old variables."

Attempting an update or insert on these collections directly will result in a runtime exception.

Immutable History

"Trigger.old is always read-only. We can't make any changes on top of it."

Use this collection exclusively for comparing previous values to detect changes during an update.

Contextual Writes

"We can use Trigger.new in the before context to update values, but not in the after context."

Modifying fields in the Before context saves automatically without a DML call. In After, the records are already locked.

Transaction Sequence

THE EXECUTION PATH

01

Before Record Flow

Initialization phase: Prime data before triggers fire.

02

Before Triggers

Validation context: Perform pre-save updates and checks.

03

System Rules

Compliance check: Execute standard and custom validation.

04

Duplicate Rules

Data Guard: Final block before staging the save.

05

Save Record

Database stage: Record is written but not committed.

06

After Triggers

Post-save logic: Coordinate related record updates.

07

After Flow

Automation chain: Execute record-triggered after flows.

08

Commit

Finalization: Data is permanently saved to the cloud.

Post-Commit Lifecycle Active

"Send Email, Async Jobs Like Future, Queueable, and Async paths in Record-Triggered Flows."