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
sss pppsss ppp 

give me scenario based example of trigger.old?

I am too confused in trigger.new and trigger.old.I fyou could give scenario based trigger example.
Best Answer chosen by sss ppp
buggs sfdcbuggs sfdc

Difference between Trigger.new and trigger.old

In simple words Trigger.new returns new value of records and Trigger.old returns old value of records.
Trigger.new works in case of insert and update both and Trigger.old works in case of update and delete both. For update you can use both as per your need.
Lets take a example: In case of Insert

trigger myTest on Scheme__c(after insert){
       for(Scheme__c sc : Trigger.new){
            // Trigger.new will return new value of records.
      }
}
 
Lets Take a Example: In Case of update and delete and undelete

Lets suppose Ammount__c field have current value equal 2000 and in updation user provide Ammount__c = 3000 Than.....

trigger myTest on scheme__c(after update,after delete,after undelete){
    for(Scheme__c sc : Trigger.old){
       System.debug('========='+sc.Ammount__c); // will return 2000
    }
    for(Scheme__c sc : Trigger.new){
         System.debug('==========='+sc.Ammount__c); // will return 3000

    }

}

For more detail follow below link:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

 

All Answers

buggs sfdcbuggs sfdc

Difference between Trigger.new and trigger.old

In simple words Trigger.new returns new value of records and Trigger.old returns old value of records.
Trigger.new works in case of insert and update both and Trigger.old works in case of update and delete both. For update you can use both as per your need.
Lets take a example: In case of Insert

trigger myTest on Scheme__c(after insert){
       for(Scheme__c sc : Trigger.new){
            // Trigger.new will return new value of records.
      }
}
 
Lets Take a Example: In Case of update and delete and undelete

Lets suppose Ammount__c field have current value equal 2000 and in updation user provide Ammount__c = 3000 Than.....

trigger myTest on scheme__c(after update,after delete,after undelete){
    for(Scheme__c sc : Trigger.old){
       System.debug('========='+sc.Ammount__c); // will return 2000
    }
    for(Scheme__c sc : Trigger.new){
         System.debug('==========='+sc.Ammount__c); // will return 3000

    }

}

For more detail follow below link:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

 
This was selected as the best answer
Dilip_VDilip_V
Hi Patil,

Trigger.new returns new or latest value of records and Trigger.old returns old value of records.
Account Acc=new Account();
Acc.name='Patil';
Insert Acc;
//Here we are Inserting account.
//For this record trigger.old will be null and trigger.new contains account information(Such as Id ans Name of Acc).
Acc.Name='Patil s';

Update Acc;
//Here we are updating account.Here Trigger.old contans Acc(Account) name as 'Patil'
//Trigger.new contains Acc(Account) name as 'Patil s'

Thanks.



 
raj_sfdccraj_sfdcc
Hi ,
Basically trigger.new contains records which are inserting into the database .and while update action ,Trigger.new contains new values after update and trigger.old contains values before update.So that we can compare new and old values.Please find the below example .

DemoTtrigger1 Populates Description field with the user first name who creates or updates the record by using userInfo standard keyword . And also does not allow user to delete the record .
 
trigger DemoTtrigger1 on Author__c (before insert,before update,before delete) {
    If(trigger.isInsert){
        for(Author__c Author: Trigger.new){
            //Populates Description with the user first name who creates the record
            Author.Description__c = 'Author Created by '+ userInfo.getFirstName(); 
        }
    } else
        If(trigger.isUpdate){
            for(Author__c Author: Trigger.new){
          //Updates Description with the user first name who updates the record
                Author.Description__c = 'Author Last updated by '+ userInfo.getFirstName();   
            }
        }
    if(Trigger.isDelete&&Trigger.isbefore){ 
        for(Author__c Rec:trigger.old) 
        {
          //User gets the below error when user tried to delete the record
            Rec.adderror('You Cannot Delete the Author Record');
        }
    }
}

Please find the below post for more examples of apex trigger with the real time scenarios .
Apex Real Time Examples (https://salessforcehacks.blogspot.com/2020/02/apex-trigger-examples-salesforce-apex.html)