Skip to main content
SFDC Developers
Apex

When we should use Trigger.Old

Vinay Vernekar · · 3 min read

Understanding Trigger.Old in Salesforce Apex Triggers

Trigger.Old is a context variable available in Salesforce Apex triggers. It provides a read-only list of the original versions of sObject records before they were changed by a DML operation. Knowing when and how to use Trigger.Old is essential for building robust, data-consistent triggers.

When to use Trigger.Old

Use Trigger.Old in these scenarios:

  • Before Update or After Update triggers: When you need to compare old and new values to detect changes. For example, tracking field changes, enforcing business rules triggered by specific field updates, or auditing.
  • Before Delete or After Delete triggers: To access the record values that are being deleted (since the current record is no longer available after deletion).
  • Cascading updates or related record handling: When changes to one record require updates to related records, comparing old and new values helps determine if related updates are necessary.
  • Preventing or allowing processing based on changes: Use Trigger.Old to check whether a particular field’s value changed and then decide whether to proceed with additional processing.

Examples

  1. Detecting a field change in a Before Update trigger:

for (Integer i = 0; i < Trigger.new.size(); i++) { Account newAcc = (Account)Trigger.new[i]; Account oldAcc = (Account)Trigger.old[i]; if (newAcc.AnnualRevenue != oldAcc.AnnualRevenue) { // Handle revenue change logic } }

  1. Using Trigger.Old in a Before Delete trigger to archive values:

for (Account acc : Trigger.old) { // Copy values to a custom archive object before deletion Account_Archive__c arch = new Account_Archive__c(); arch.Name__c = acc.Name; arch.Revenue__c = acc.AnnualRevenue; inserts.add(arch); }

Important considerations

  • Trigger.Old is only available in update and delete contexts — it is not available in insert triggers.
  • The lists Trigger.new and Trigger.old correspond by index, so use the same index when comparing records.
  • Trigger.Old is read-only. To change data, modify Trigger.new records (for before triggers) or perform DML operations on other sObjects.
  • Watch for bulk operations — always write triggers that handle collections efficiently to avoid governor limits.

Best practices

  • Use Maps keyed by Id when comparing related records to improve performance in bulk operations.
  • Keep trigger logic minimal by delegating complex processing to handler classes.
  • Avoid SOQL or DML inside loops — bulkify your code.
  • Write unit tests that simulate update and delete contexts to verify Trigger.Old behavior.

Using Trigger.Old correctly helps you build predictable, maintainable Apex triggers that respond accurately to record changes.

Share this article

Vinay Vernekar

Vinay Vernekar

Salesforce Developer & Founder

Vinay is a seasoned Salesforce developer with over a decade of experience building enterprise solutions on the Salesforce platform. He founded SFDCDevelopers.com to share practical tutorials, best practices, and career guidance with the global Salesforce community.

Get weekly Salesforce dev tutorials in your inbox

Comments

Loading comments...

Leave a Comment

Trending Now