Trigger.new stores list of new records which are going to be inserted and updated into particular object. Trigger.old stores list of old records which are updated with new values, which are going to be deleted from database.
Example: I have created one custom object student and it has Name & Teacher custom fields. and if I try to insert new record what will happen..
Trigger: I have written trigger to check what would be there in Trigger.new & Trigger.old when trigger event occurs. First of all we have to enable Debug Logs in our Organization, so that only we can check.
trigger exampl26 on Student__c (Before Insert, After Insert, Before Update, After Update, Before Delete, After Delete, after undelete) {
If(trigger.isBefore)
{
if(trigger.isInsert)
{
system.debug('Before Insert Trigger.new is---------- :'+trigger.new);
system.debug('Before Insert trigger.old is------------ :' +trigger.old);
}
if(trigger.isupdate)
{
system.debug('Before Update Trigger.new is---------- :'+trigger.new);
system.debug('Before Update trigger.old is------------ :' +trigger.old);
}
if(trigger.isDelete)
{
system.debug('Before Delete Trigger.new is---------- :'+trigger.new);
system.debug('Before Delete trigger.old is------------ :' +trigger.old);
}
}
If(trigger.isafter)
{
if(trigger.isInsert)
{
system.debug('after Insert Trigger.new is---------- :'+trigger.new);
system.debug('after Insert trigger.old is------------ :' +trigger.old);
}
if(trigger.isupdate)
{
system.debug('after Update Trigger.new is---------- :'+trigger.new);
system.debug('after Update trigger.old is------------ :' +trigger.old);
}
if(trigger.isDelete)
{
system.debug('after Delete Trigger.new is---------- :'+trigger.new);
system.debug('after Delete trigger.old is------------ :' +trigger.old);
}
if(trigger.isundelete)
{
system.debug('after Undeletete Trigger.new is---------- :'+trigger.new);
system.debug('after Undelete trigger.old is------------ :' +trigger.old);
}
}
}
Before Insert:
Trigger.New: (Student__c:{Id=null, Name=venu, Teacher__c=a0328000000nFUFAA2})
Trigger.Old: NULL
After Insert: After record inserted to Database
Trigger.new: (Student__c:{Id=a0128000017bQrcAAE, Name=venu, Teacher__c=a0328000000nFUFAA2})
Trigger.old: NULL
Before Update: i was trying to updated Student Name Venu to 'Venu Gutta' then
Trigger.new: (Student__c:{Id=a0128000017bQrcAAE, Name=venu Gutta, Teacher__c=a0328000000nFUFAA2})
Trigger.old: (Student__c:{Id=a0128000017bQrcAAE, Name=venu, Teacher__c=a0328000000nFUFAA2})
After Update:
Trigger.new: (Student__c:{Id=a0128000017bQrcAAE, Name=venu Gutta, Teacher__c=a0328000000nFUFAA2})
Trigger.old: (Student__c:{Id=a0128000017bQrcAAE, Name=venu, Teacher__c=a0328000000nFUFAA2})
if I delete the That Record
Before Delete:
Trigger.new: Null
Trigger.old: (Student__c:{Id=a0128000017bQrcAAE, Name=venu Gutta, Teacher__c=a0328000000nFUFAA2})
After Delete:
Trigger.new: Null
Trigger.old: (Student__c:{Id=a0128000017bQrcAAE, Name=venu Gutta, Teacher__c=a0328000000nFUFAA2})
if I undelete that record from Recylic bin then:
After Undelete:
Trigger.new: (Student__c:{Id=a0128000017bQrcAAE, Name=venu Gutta, Teacher__c=a0328000000nFUFAA2})
Trigger.old: NULL
Click Here for Basic example trigger for Difference Between
Trigger.New and Trigger.old
Difference Between Before and After Triggers In Salesforce
No Comment to " Difference Between Trigger.new & Trigger.old in All Trigger Events with Example "