+ Start a Discussion

Trigger.newMap Vs Trigger.oldMap

i need programatic code  explantion ple any one have ideal send me .




Suppose you have an opportunity object. And you want to trigger on Stage field.

You have after/before update trigger on opportunity, which should be fired only when stage is changed. I this case we can use Trigger.Old. See below example


//Trigger.new: Contains all new Data
//Trigger.Old: Contains all old Data (before change)
//Trigger.newMap: Map contains new Data (Key: Id)
//Trigger.oldMap: Map contains old Data (Key: Id)
for(Opportunity opp:Trigger.New){
	if(opp.Stage != Trigger.oldMap.get(opp.Id).Stage){
		//Your logic
//Same way you can use Trigger.newMap


I hope this will helpfull to you. Let me know if you have more question.

k Rajaprashanthk Rajaprashanth
you can see in the below code i want to compare existing Account "credit status(picklist )" and newly update "credit status" if both the vales doesnt match then i want to update some fields as i mentioned in the  below code 

Trigger.OldMap: Trigger.oldMap returns map of old records which are updated with new values. These List of records already there in Database. Trigger.oldmap available in Before update, after update, Before Delete and After Delete triggers.

Trigger.NewMap: Trigger.newMap returns map of new records which are trying to insert into Database. This is available in Before Insert, Before Update, After Insert,  After Update Triggers and undelete Triggers. This list of records can only modified in Before triggers.

for(Account Acc:trigger.new){
       Account oldaccount = new Account();
        oldaccount = trigger.oldmap.get(Acc.id); // trigger old map to get old value(existing value) of the record 
        if(oldaccount.CreditStatus__c !=Acc.CreditStatus__c ){
            Acc.CreditstatusDate__c = system.today();