Understanding Trigger Context Variable

When I started my carrier as Salesforce developer.when I wrote my first trigger code, I always use to hear "first understand Trigger.new and Trigger.old"(Trigger context variable) from senior colleague in my team. Then I started searching for what trigger.new, trigger.old and other context variable will return in trigger, whether there is any practical experiment for this, but I didn't get any. 

This is only one Salesforce site helped me to understand theoretically.

So,I think why can't write blog in this topic with some practical experiment.Many of starting phase developer will face trouble to understand trigger context variable clearly, then I researched.Now its for your learning purpose.
            
Demo:
    1.To give a demo I created one custom object called "Book" with two custom fields one is "Author" with data type test and one more is "Price" with data type currency.
    
    2.Create a Custom object tab called book.
    
    3.After created these object and fields don't create any records.Create trigger in the name of "TriggerContextVarDemo" copy paste the below code in that trigger and save it.

Trigger Code: 

Please go through  comments in the code. It will help you to understand.

trigger TriggerContextVarDemo on Book__c (before insert, after insert, before update, after update)
{
      /*This if block will run only before insert.It means mean time of 
        the records going to save into salesforce database*/
         
      if(Trigger.isInsert && Trigger.isBefore){
            System.debug('****Insert before Trigger.new******'+Trigger.new);
            /*Trigger.New-new versions of the sObject records.
              e.g. It will work both insert and update for insert every record will be new.
              creating 1st record on book it will return 1st record value,creating 2nd will return second
              record value like that it will go. 
            */
             
            System.debug('****Insert before Trigger.old******'+Trigger.old);
            /*Trigger.Old-old versions of the sObject records.
              this will not work both before and after insert.Then always the value will be null.
               because there will be no old version(old value) of the record, all are new record.
               old vesion in the sence while updating any record you may change some field value to new value .
               that old value with record will be in trigger.old.
            */
             
            System.debug('****Insert before Trigger new map******'+Trigger.newMap);
            /*Trigger.newMap-map of IDs to the new versions of the sObject records.
              It will work only on after insert because new map must store the Id and record.
              In the before insert how the id will be generated.
              so always it will return null.
            */
              System.debug('****Insert before Trigger old map******'+Trigger.oldMap);
              /*It will not return any value always its null*/
    }
    
    /*This if block will only run after succesful insert of records into salesforce database*/
         
    if(Trigger.isInsert && Trigger.isAfter){
            System.debug('****Insert after Trigger.new******'+Trigger.new);
            /*It will return same value as explained before insert of Trigger.new*/
             
            System.debug('****Insert after Trigger.old******'+Trigger.old);
            /* this will not work both before and after insert.Then always the value will be null.
               because the will be no old version(old value) of the record all are new record
               old vesion in the sence while updating any record you may change some field value to new value .
               that old value will be in trigger.old
            */
            System.debug('****Insert after Trigger new map******'+Trigger.newMap);
           /*It will return new map value as explained before insert of Trigger.newMap
             because id is generated for new record that is saved.
           */
           System.debug('****Insert after Trigger old map******'+Trigger.oldMap);
            /*It will not return any value always its null */
    }
     
      /*This if block will run only before update.It means mean time of 
        the records going to update into salesforce database*/
         
    if(Trigger.isupdate && Trigger.isBefore){
            System.debug('****update before Trigger.new******'+Trigger.new);
            /*As I explained previously Trigger.new is the new version of sobject record
              create first book record with price 200 then edit and change price 600 and click save
              now this Trigger.new will contain the new value of record that means price  value with 600 record
           */
            System.debug('****update before Trigger.old******'+Trigger.old);
             /*As I explained previously Trigger.new is the new version of sobject record
              Edit the created book record and change price from 200 to 600 and click save
              now this Trigger.old will contain the old value of record that means price value with 200 record
           */
            System.debug('****update before Trigger new map******'+Trigger.newMap);
            /*As I explained previously Trigger.new is the new version of sobject record
              create first book record with price 200 then edit and change price 600 and click save
              now this Trigger.newMap will contain the newmap value of record that means price  value with 600 record
           */
            System.debug('****update before Trigger old map******'+Trigger.oldMap);
             /*As I explained previously Trigger.new is the new version of sobject record
              Edit the created book record and change price from 200 to 600 and click save
              now this Trigger.oldMap will contain the oldMap value of record that means price value with 200 record
           */
   }
     
    /*This if block will only run after succesful update of records into salesforce database*/
    //This block will return same value as returned in is before update
     if(Trigger.isupdate && Trigger.isAfter){
            System.debug('****update after Trigger.new******'+Trigger.new);
            System.debug('****update after Trigger.old******'+Trigger.old);
            System.debug('****update after Trigger new map******'+Trigger.newMap);
            System.debug('****update after  Trigger old map******'+Trigger.oldMap);
 
    }
 
}

 

4. Before going to explain the code if you are not aware of the developer console just get to know what is "Developer Console" and how to see the logs.To view developer console fallow below navigation
         User Menu(Top right hand side your name drop down)--> Click Developer Console.

 5.Now open developer console in new window.And create the first book record from Book tab in another window. 
                  Book Name :Lost Symbol
                  Prices : 200
                  Author :Dan Brown

        Now only first two  IF block  in  the code will executed because the record creation is an insert operation. Then see your developer console tab, one log will be recorded. You can see it like below window.

 6.Click -->log and click--> open raw log button below log area section.Now check the value as I explained in the below line of each system.debug().
       
      Before insert block result:

After insert block result:

7.I think now you are clear about Trigger.new and  Trigger.old etc in both before and after insert.
Create one more record and check it. 
       8.Now in the developer console clear the log by using clear button.
       9.Edit the first created book record and change  price value from 200 to 600 and save it now open the developer console tab and check the values each system.debug(). You can notice price 200 record in Trigger.old and 600 is in Trigger.one. 
       10.Now only third and fourth IF block  in  the code will executed because the record edit and save is an update operation. Then see your developer console tab, one log will be recorded. You can see it like below window.
      
     Before update block result:

After update block result:

I think know you all are clear about the Trigger.new and Trigger.old .And the use of isInsert, isUpdat

Rating: 
Average: 5 (1 vote)