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
sonali  vermasonali verma 

trigger.old

Hello
I want to understand critical differences between trigger.new & trigger.old.
trigger.old used in which events?
I see lot of documentation but any specific scenario will be helpful​​

thanks
sonali​​
Navee RahulNavee Rahul
Hello sonali,

basically the Event would be u Specifing as Argument
 
trigger MyTrigger on Order (before update,after update)
where the trigger events will be before update,after update.

trigger.new will holds the NEW  Values(Data) for the Sobject fields you have Enterted.

trigger.old will HOLDS the OLD Values(Data) for the Sobject fields which already exist(trigger.old will be Draft ).

Consider u were trying to wtrite a Trigger for Order .

Criteria :when Order status changes from Draft to Activated.

1.Trigger.New will Holds the Sobject  values for Draft Order record.
   trigger.new[i].Status will Activated

2.Trigger.OLD will Holds the Sobject  values for Draft Order record.
   trigger.OLD[i].Status will DRAFT.

  trigger.OLD[i].Status will NULL if you were creating record for the first time and if Default is not specified.




Thanks
D Naveen rahul.

 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. for Understanding Trigger.old and Trigger.new values in before & after update triggers
https://help.salesforce.com/apex/HTViewSolution?id=000003789&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000003789&language=en_US)

Please check below post for Trigger Context Variables
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm
isExecuting Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or anexecuteanonymous() API call.
isInsert Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or theAPI.
isUpdate Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or theAPI.
isDelete Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or theAPI.
isBefore Returns true if this trigger was fired before any record was saved.
isAfter Returns true if this trigger was fired after all records were saved.
isUndelete Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)
new Returns a list of the new versions of the sObject records.
newMap A map of IDs to the new versions of the sObject records.
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.
oldMap A map of IDs to the old versions of the sObject records.
size The total number of records in a trigger invocation, both old and new.

Comparing old and new values in a trigger
http://www.sfdc99.com/2014/02/25/comparing-old-and-new-values-in-a-trigger/
// Check a checkbox only when an Opp is changed to Closed Won!
trigger Winning on Opportunity (before update) {
  for (Opportunity opp : Trigger.new) {
    // Access the "old" record by its ID in Trigger.oldMap
    Opportunity oldOpp = Trigger.oldMap.get(opp.Id);

    // Trigger.new records are conveniently the "new" versions!
    Boolean oldOppIsWon = oldOpp.StageName.equals('Closed Won');
    Boolean newOppIsWon = opp.StageName.equals('Closed Won');
    
    // Check that the field was changed to the correct value
    if (!oldOppIsWon && newOppIsWon) {
      opp.I_am_Awesome__c = true;
    }
  }
}

Trigger.new : Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert andupdate 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 anddelete triggers.

Please let us know if this will help you

Thanks
AMit Chaudhary
 
sonali  vermasonali verma
Hi
I am clear with the above.
But pls clarify.
a) crucial differences between Trigger.oldmap and trigger.newmap.
    which scneario we use
b) can I use before delete evnt in trigger.oldmap

thanks
sonali​​​​​​
Amit Chaudhary 8Amit Chaudhary 8

a) crucial differences between Trigger.oldmap and trigger.newmap.
    which scneario we use

OldMap and NewMap is same like Trigger.Old and Trigger.New with key value paire
In Trigger.Old you will get list of Sobject but on Trigger.OldMap you will get Map<ID,Sobject>.

b) can I use before delete evnt in trigger.oldmap

Yes you can use the Trigger.OldMap and Trigger.Old in before delete.
 
sonali  vermasonali verma
Hi
In trigger.oldmap we can use afterUpdate event.
so by this time RecordID is available.
Then ​w​hy an update to the original object using DML statement would cause recursion / or hit governor limit:?


thanks
sonali​​​
 
Amit Chaudhary 8Amit Chaudhary 8
Yes in After Update or before Update trigger we can user old and oldMap. I will not impact on governor limit.
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Never update the same record in After Update in that case your trigger can become the recursive which can raise the 101 SOQL issue
But that 
one you can stop by static variable. Please check post for same.
http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html

Please let us know if this will help you

Thanks,
Amit Chaudhary