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
SimrinSimrin 

get value of a column before and after update of row using trigger

Hello,

I want to get the value of a column before and after the update.
trigger test on Custom_Object__c (after insert, after update, before delete) {
    
    List <Custom_Object__c> item = new List <Custom_Object__c>();
    
    for (Custom_Object__c w: trigger.new) {
        String Column1 = w.Column1__c;
    }
}

In the above trigger i get the value of Column1__c after update.

I assume that doing below changes will get me value before update
trigger test on Custom_Object__c (before insert, before update, before delete) {
	
}

I want to detect values in a single trigger that what is the value befpre update and after update.
Is there any way to do it.


Best Answer chosen by Simrin
Le NguyenLe Nguyen
Hi Simrin,

You can get the old values before update by getting it from trigger.oldmap.

Sample code:
trigger test on Custom_Object__c (after insert, after update, before delete) {
    if(Trigger.isUpdate){
	    List <Custom_Object__c> item = new List <Custom_Object__c>();
	    for (Custom_Object__c w: trigger.new) {
	    	Custom_Object__c ow = trigger.oldmap.get(w.id);// get the object before update
	        String Column1OldValue = ow.Column1__c;
	    }
    }
}

Regards,

Le

All Answers

KaranrajKaranraj
Simrin - You can use following trigger context variable in your trigger to identify the excution and assign the value to the variable.
  • isInsert -  Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API.
  • isUpdate  - Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.
  • isDelete - Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.
  • 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 theAPI.)
 
trigger test on Custom_Object__c (after insert, after update, before insert, before update) {
    if(isBefore){
       if(isInsert){
       //This part will be excuted only in the Before insert Operation
       }
       if(isUpdate){
         //This part will be excuted only in the Before update Operation
       }
    }else if(isAfter){
      if(isInsert){   
        //This part will be excuted only in the After insert Operation
      }   
      if(isUpdate){
       //This part will be excuted only in the After update Operation
      }
    }
   
}

Thanks,
Karanraj
 
Le NguyenLe Nguyen
Hi Simrin,

You can get the old values before update by getting it from trigger.oldmap.

Sample code:
trigger test on Custom_Object__c (after insert, after update, before delete) {
    if(Trigger.isUpdate){
	    List <Custom_Object__c> item = new List <Custom_Object__c>();
	    for (Custom_Object__c w: trigger.new) {
	    	Custom_Object__c ow = trigger.oldmap.get(w.id);// get the object before update
	        String Column1OldValue = ow.Column1__c;
	    }
    }
}

Regards,

Le
This was selected as the best answer