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/update 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.
Before Insert Trigger
-----------------------
Trigger.new- What are the records going to insert into Database [not commited yet, id=null]
trigger.old- NULL
Trigger.newmap-null
Trigger.old-Null
Afeter Insert
-----------------------------
Trigger.new- New List of Records which are inserted
Trigger.old- null
Trigger.NewMap- New Map<ID, new Record>
Trigger.oldMap- Null
Before Update
------------------------------
Trigger.new- New List of Records which are going to be updated
Trigger.old- List of Records with old values.
Trigger.NewMap- New Map<ID, new Record>
Trigger.oldMap- Old Map<ID, old record>
After Update
-------------------------------
Trigger.new- New List of Records which are updated
Trigger.old- List of Records with old values.
Trigger.NewMap- New Map<ID, new Record>
Trigger.oldMap- Old Map<ID, old record>
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 and New phone number are not same then Description field will be updated with Old phone number+New phone number. if 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 ;
}
}
}
Here i have created one new Account record with phone number 7799703404
I have updated phone number 7799703404 with new phone number 8897125248 and saved the record. Then description field automatically updated with "old phone number is (779) 970-3404 and New Phone number is (889) 712-5248".
In above Example Trigger.new record holds new phone number 887125248 and Trigger.old holds old phone number 7799703404.
Trigger.New: Trigger.new returns List of new records which are trying to insert/update 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.
Before Insert Trigger
-----------------------
Trigger.new- What are the records going to insert into Database [not commited yet, id=null]
trigger.old- NULL
Trigger.newmap-null
Trigger.old-Null
Afeter Insert
-----------------------------
Trigger.new- New List of Records which are inserted
Trigger.old- null
Trigger.NewMap- New Map<ID, new Record>
Trigger.oldMap- Null
Before Update
------------------------------
Trigger.new- New List of Records which are going to be updated
Trigger.old- List of Records with old values.
Trigger.NewMap- New Map<ID, new Record>
Trigger.oldMap- Old Map<ID, old record>
After Update
-------------------------------
Trigger.new- New List of Records which are updated
Trigger.old- List of Records with old values.
Trigger.NewMap- New Map<ID, new Record>
Trigger.oldMap- Old Map<ID, old record>
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 and New phone number are not same then Description field will be updated with Old phone number+New phone number. if 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 ;
}
}
}
Here i have created one new Account record with phone number 7799703404
I have updated phone number 7799703404 with new phone number 8897125248 and saved the record. Then description field automatically updated with "old phone number is (779) 970-3404 and New Phone number is (889) 712-5248".
In above Example Trigger.new record holds new phone number 887125248 and Trigger.old holds old phone number 7799703404.