+ Start a Discussion
Shree KShree K 

When to use Trigger.new and when to use trigger.old for Update operation in triggers

 
Hi 
I am little confused about certain things like,
Trigger.new works on both Insert and update Operations and Trigger.old works on update and delete operation.so here Update opearation is common in trigger.new and trigger.old 
 up to my knowledge update means ,overriding an existing value and replacing it with new value that means it has to retrieve the old values and  it should hold new values to override the old values regardless of if it is an Before update or after update, here i am confused about when to use trigger.new and when to use trigger.old particularly for update operation, and lastly

Does trigger.new works on After events such as After Insert,after Update.
Does trigger.old works on Before events on any operation(insert/update) in any way
I have been to the docs but i got confused.
Best Answer chosen by Shree K
Shashikant SharmaShashikant Sharma
Please see my answers:


Scenario 1:when ever a record is going to be updated on countries Obj with name field as "india" another field named Capital on the same object should be updated with "Delhi" value,
so this is a Before Update operation (here trigger fired due to an Updated operation and it caused another update on capital field within the sameObject) so this worked based on Trigger.New and Finally

[Shashikant]: Yes thats correct. 

Scenario 2: here two related objects countries and Economy now the if the requirement is, due to the earlier update operation on the Countries Object there should be an update on Currency field with value "INR" on the Economy Object(related object to countries), 
so this is an After update operation (trigger fired after update operation on Countries andcaused another update on Economy object ) and this worked based on Trigger.Old.
i Doubt, no matter if it is an Before update or After update does the complete Update operation works based on both Trigger.New and Trigger.old
at a time or only on one thing either trigger.new or trigger.old at a time.

[Shashikant]: In this scenario trigger was invoked due to update on countries but you want to udpate another object so your trigger will be on after update event, first you will check whether Country name has been updated and then will update economy object record. In order to check whether name field is updated or not you need both trigger.new and trigger.old as you will need to compante new and old value and if there is a difference then you will know that Economy needs to be modified.

To know more about triggers you cuold read : http://forceschool.blogspot.in/search/label/Apex%20Triggers

Thanks
Shashikant
 

All Answers

Shashikant SharmaShashikant Sharma

trigger.new - is collection of records with new values due to DML Activity 
trigger.old - is collection of records with old values due to DML Activity 

When to use:

Trigger.New - If you are to update any value on the record which got updated ex- Set Name of as

Opprtunity Name = Account Name + Created Date then use trigger.new

Trigger.Old - 

1. Need to check if any fied is update in the DML Activity then do another update, like if accont name gets changed then update related opportunity name 

in above case you will check 

if( acc.name != trigger.oldMap(acc.Id).Name )

trigger.oldMap(acc.Id).Name will give you old value of name of the record 


2. Trigger.Old is used in Delete DML activity triggers as well as there is no new value gets updated during a delete operation.

Thanks
Shashikant

  

Vivek DeshmaneVivek Deshmane
Hi,
Please go through below points and let me know if this helps you.
Trigger.new
  • In After insert and after update triggger.new collection will holds latest values and this collection is read olny  means we can't modify record data using this collection .
  • Howevere we can take record Id of curret record and query it again from DB and will perform update operation if require.
  • Trigger.new is not available in delete  trigger 
  • Trigger.new is availabe in after undelete Trigger.
Trigger.old
  •  In Before Insert Trigger.old collection is not able, it's null.
  • Trigger.old trigger is available in after update trigger and will hold old copy of current modifying record.
  • Trigger.old is available in Delete trigger .
Best Regards,
-Vivek
Shree KShree K

Thanks @Shashikant Sharma and @Vivek Deshmane your answers helped me alot, i have a last that will break my confusion forever
The only thing i am confused is about Update operation
 
Scenario 1:when ever a record is going to be updated on countries Obj with name field as "india" another field named Capital on the same object should be updated with "Delhi" value,
so this is a Before Update operation (here trigger fired due to an Updated operation and it caused another update on capital field within the sameObject) so this worked based on Trigger.New and Finally

Scenario 2: here two related objects countries and Economy now the if the requirement is, due to the earlier update operation on the Countries Object there should be an update on Currency field with value "INR" on the Economy Object(related object to countries), 
so this is an After update operation (trigger fired after update operation on Countries andcaused another update on Economy object ) and this worked based on Trigger.Old.
i Doubt, no matter if it is an Before update or After update does the complete Update operation works based on both Trigger.New and Trigger.old
at a time or only on one thing either trigger.new or trigger.old at a time.
 
This is what i understood so far,sorry if i am lame, am i seeing the things in right direction. or else please provide me a simple example about how trigger.new and trigger.old works on before Update and after update operation, iam only confused Update Realated to T.new and T.old.
Shashikant SharmaShashikant Sharma
Please see my answers:


Scenario 1:when ever a record is going to be updated on countries Obj with name field as "india" another field named Capital on the same object should be updated with "Delhi" value,
so this is a Before Update operation (here trigger fired due to an Updated operation and it caused another update on capital field within the sameObject) so this worked based on Trigger.New and Finally

[Shashikant]: Yes thats correct. 

Scenario 2: here two related objects countries and Economy now the if the requirement is, due to the earlier update operation on the Countries Object there should be an update on Currency field with value "INR" on the Economy Object(related object to countries), 
so this is an After update operation (trigger fired after update operation on Countries andcaused another update on Economy object ) and this worked based on Trigger.Old.
i Doubt, no matter if it is an Before update or After update does the complete Update operation works based on both Trigger.New and Trigger.old
at a time or only on one thing either trigger.new or trigger.old at a time.

[Shashikant]: In this scenario trigger was invoked due to update on countries but you want to udpate another object so your trigger will be on after update event, first you will check whether Country name has been updated and then will update economy object record. In order to check whether name field is updated or not you need both trigger.new and trigger.old as you will need to compante new and old value and if there is a difference then you will know that Economy needs to be modified.

To know more about triggers you cuold read : http://forceschool.blogspot.in/search/label/Apex%20Triggers

Thanks
Shashikant
 
This was selected as the best answer