function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PatcsPatcs 

what is the difference between "Trigger.New" and "Trigger.old"?

what is the difference between "Trigger.New" and "Trigger.old"?

Best Answer chosen by Admin (Salesforce Developers) 
Pradeep_NavatarPradeep_Navatar

Trigger.new : Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.

 

Trigger.old : Returns a list of the old versions of the sObject records. Note that this sObject list is only available in update and delete triggers.

 

For more info go through this link:

 http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf

All Answers

kritinkritin
trigger.new is holding your object record data which is currently set by user and trigger.old contains history object records. like if we have Opportunity trigger. and in this Opportunity.trigger we have even after update and if you need to compute if there is any change in Opportunity stage then you can figure out this changes using trigger.new and trigger.old values.
BaLab2aBaLab2a
Pradeep_NavatarPradeep_Navatar

Trigger.new : Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.

 

Trigger.old : Returns a list of the old versions of the sObject records. Note that this sObject list is only available in update and delete triggers.

 

For more info go through this link:

 http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf

This was selected as the best answer
SFDCnerdSFDCnerd

Suppose i am writing a trigger on Account object and the trigger fires before/after a single/many record(s) is/are updated. Now trigger.new states Returns a list of the new versions of the sObject records. Can someone please explainn what trigger.new will contain? Can i see this list using system.debug in developer console?

 

Many Thanks in advance.

 

Thomas DvornikThomas Dvornik

Better yet, set a heap dump to see the list of values in a tree. 

 

In the developer console, create this trigger.

 

trigger MyTrigger on Account (before update) {
    List<Account> newTriggers = Trigger.new;
    List<Account> oldTriggers = Trigger.old;
    System.debug('We want to set the heap dump marker here');
}

 On line 4 (the debug statement) click on the gutter to create a heap dump location. You will see it listed in the Heap Dump tab.

 

Then execute anonymous the following.

 

Account a = [Select Name from account limit 1];
a.name = 'Test Name';
update a;

When that is done executing, you will see a new heap dump appear (like a log) in the Heap Dump tab. Open it, then on the Symbols tab, double click on "this", then double click on "newTrigger" then double click on "0". You will see Name is set to "Test Name". Do the same for "oldTrigger" and you will see the previous value. For me it was just "test". 

 

Hope that helps.

SFDCnerdSFDCnerd
Thanks a lot Thomas for a quick response..Its clear to me now....i did it with 2 rows and its fine...i will do the same for oldmap and newmap as well...i guess it would be messy when i do this on bulk triggers [?]
Thomas DvornikThomas Dvornik

I'm not sure what you mean by "i will do the same for oldmap and newmap". You typically only need to modify the new map. For example, lets say you want to prepend a value to the account name. You would do something like the following.

 

trigger MyTrigger on Account (before update) {
    for (Account account : Trigger.new) {
        account.name = 'PRE-'+account.name;
    }
}

 You would use the old map is to see when a field has changed, or to compute some value using the two, for example:

 

trigger MyAccount2 on Account (before update) {
    for (Account account : Trigger.new) {
        Account oldAccount = Trigger.oldMap.get(account.ID);
        if (account.Name.equals(oldAccount.Name)) {
            // Fields was updated - Do what you need to do
            // For example, compute a new field
            // account.NameSizeDelta__c = oldAccount.name.length() - account.name.length();
        }
    }
}

 

praveen murugesanpraveen murugesan

Thanks yar.. Thanks for your information.

praveen murugesanpraveen murugesan
Thanks for your information.
Nguyen Chi LinhNguyen Chi Linh
Thanks for infomations
Atul Pandey 4Atul Pandey 4
Thanks a lot Thomas 
smriti sharan19smriti sharan19
Trigger.new - It returns the list of new version of sobject records. This is only available for insert and update trigger.
Trigger.old- It returns the list of  old version of sobject records. This is only avaiable in update and delete trigger.

For example you want to add value to the account record. Here you will use trigger.new

Trigger.old you can use to see the value when the field is changed.
U VenuU Venu
Hi
Pacts,

Trigger.new and Trigger.old both are context variables in Salesforce. 

Trigger.New: Trigger.new returns List of new records which are trying to insert into Database. This is available in Before Insert, Before Update, After Insert,  After Update Triggers and undelete Triggers. This list of records can only modified in Before triggers.

Trigger.Old: Trigger.old returns List of old records which are updated with new values. These List of records already there in Database. Trigger.old available in Before update, after update, Before Delete and After Delete triggers.

Example: I have a scenario like when Account Phone number is updated then Account Description value should be updated with old phone number+new phone number.

trigger NewVsOld on Account (before update) {
    for(integer i=0; i<trigger.new.size();i++){
/*if Old Phone number andNew phone number are not same then Description field will be updated with Old phone number+New phone number. if not same no change will be in Description field*/
    if(trigger.old[i].phone!=trigger.new[i].phone){
        trigger.new[i].description='old phone number is  ' + trigger.old[i].phone + ' and New Phone number is ' +trigger.new[i].phone ;
   }
 }


       Thank you,
           Venu.

 
Navneet Kumar 93Navneet Kumar 93
@Thomas Dvornik..i am not able to see "Heap Dump" tab to check the new heap dump..Please guide us if possible.