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
TarentTarent 

difference between "Trigger.New" and "Trigger.old".

difference between "Trigger.New" and "Trigger.old".
Navatar_DbSupNavatar_DbSup

Hi,

 

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 detail follow below link:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

anvesh@force.comanvesh@force.com

what is the meaning of "new version of" sobject and "old version" of sobject.....what is version meaning there.please  explain what operation happening when trigger.new?or old?

sastry.soft1.3935945807637227E12sastry.soft1.3935945807637227E12
please give a detail explaination than copy pasting the defination...okei..tell me what is the value in old.new when iam trying to delete 4 records out of 10 records in an object
Lalit_RautelaLalit_Rautela
Difference between Trigger.new and trigger.old

In simple words Trigger.new returns new value of records and Trigger.old returns old value of records. Trigger.new works in case of insert and update both and Trigger.old works in case of update and delete both. For update you can use both as per your need.
Lets take a example: In case of Insert
 
trigger myTest on Scheme__c(after insert){
       for(Scheme__c sc : Trigger.new){
            // Trigger.new will return new value of records.
      }
}

Lets Take a Example: In Case of update and delete and undelete
 
Lets suppose Ammount__c field have current value equal 2000 and in updation user        provide Ammount__c = 3000 Than.....

trigger myTest on scheme__c(after update,after delete,after undelete){
    for(Scheme__c sc : Trigger.old){
       System.debug('========='+sc.Ammount__c); // will return 2000
    }
    for(Scheme__c sc : Trigger.new){
         System.debug('==========='+sc.Ammount__c); // will return 3000

    }

}

 
praveen jha 29praveen jha 29
@Lalit_Rautela:- Good answer with example. Best Answer from my end
 
Cloud_forceCloud_force
trigger.new and trigger.old are both basically lists.
trigger.new gives your records that are currently being modified and has field values that will be updated/inserted.
trigger.old on the other hand have field values that are older ones and will be updated in the current transaction with values that are present in trigger.new
trigger.old is not available during insert.
ChinnoyChinnoy

Trigger.New and Trigger.Old are both the context Variables which returns records in List's.
Trigger.New => works for the NEW values that are entering either it may be Insert or Update.
Trigger.Old=> works for the OLD values that are already in the Fields, it may be to Delete or Update the records.

Prashant Pandey07Prashant Pandey07
Old trigger Eg:


trigger Oldtrigger on Contact (before update,after insert) {
    for (Contact account : Trigger.new) {
        Contact oldAccount = Trigger.oldMap.get(account.ID);
        system.debug('#####'+oldAccount);
        
    }
}

New trigger Eg:

trigger Oppsamename on Contact (After insert,after delete,before update) {
 

    List<Opportunity> Opp = new List <Opportunity>();
Account aa=new Account();

    For (Contact A: Trigger.new){
    system.debug('$$$$$$$$'+a);
}
}

 
Mayank RanaMayank Rana
Best Explaination of Difference between Trigger.new and trigger.old.... as per my understandings @Lalit_Rautela
Nitesh K.Nitesh K.

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 detail follow below link:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm
 
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 
Navya chittaNavya chitta
 i created custom object "employee" .
after insertion of a record ,i created a new record of contact with newly inserted employee record.
my requirement is while deleteing employee record ,i want to delete that regording contact record also.
help me  ,how can i do........
Ravikiran gudlaRavikiran gudla
According to my understanding. Trigger.New provides list of records after some operation(either insert or update).
Then how can we use Trigger.New when before insert and before update events are arguments for our trigger?

Eg: trigger MyTrigger on Account (before insert, before update) {
    for(Account a : Trigger.new)
    {
        //logic
    } 

In the above example, what will trigger.new return ? As the insert or update operation still not yet processed..
Help me to understand this.
smriti sharan19smriti sharan19
Trigger.new = returns the list of new version of sobject records. It can be used for insert and update
Trigger.olf = returns the list of old version of sobject records. It can be used for delete and update.
Ajinkya DhasAjinkya Dhas
Hi,
Please Check out this amazing blog about apex trigger
https://www.salesforcekid.com/2019/06/salesforce-apex-trigger-basics.html

Thanks
Sandeep KommulaSandeep Kommula
Trigger.new  returns list of the new versions of the sObject records. This list available for insert, update and undelete triggers.
In insert operation >> it is list of records which are going to save.
In update operation >> it is list of records which will replace old records
In Undelete operation >> it is list of records retrieved from recycle bin
In Delete operation >> there is no incoming records hence there is no trigger.new

Trigger.old returns list of old versions of the sObject records.This list available for update and delete triggers.
It represents list records already saved in Salesforce.
for insertion operation>> there is no trigger.old as there is no existing records.
In update operation >> it is list of records which are going to change.
In Delete operation >> it is list of records which are going to delete.
 
Karan JoshiKaran Joshi
Hi,
As per my understanding whenever new records are inserted or older records are updated, it can be accessed by the trigger.new but remember that trigger.new won't allow you to perform delete operation 
Whereas whenever any DML operation performed on existing records (except insert operation), you can perform the same with the help of the trigger.old but also remember that trigger.old won't allow you to perform insert operation

You can use either trigger.new or trigger.old for update operation but there are some scenarios when to use which one for this you can refer to:
https://developer.salesforce.com/forums/?id=906F0000000BQmoIAG
 
Suraj Tripathi 47Suraj Tripathi 47
Hello Shubham,

Greetings,

trigger.old Trigger.old returns old value of records and trigger.new returns new value of records. 


If you find your Solution then mark this as the best answer.

Thank you!

Regards,
Suraj Tripathi