+ Start a Discussion
balaji aluri 16balaji aluri 16 

While updating any record i need to take backup that record details in Test obj.

Hi,  I am new to salesforce development. We are having 'Payee__c' Object in our org and while i am updating any Payee record that previous values should be stored in "Test__c' Obj with a new record creation. Pls go with following code for reference. The code is executing with no errors, but while updating Payee record the old values not storing in Test obj. Quick response will be appreciated. 

trigger TestRecordInsert on Payee__c (Before Update) {
    List<Test__c> test= new List<Test__c>();
    For(Payee__c p:Trigger.old){
        Test__c t=new Test__c();
        t.Amount1__c = p.Amount__c;
        t.Payee_amount__c = p.Payee_amount__c;
        t.payee_name__c = p.payee_name__c;
        test.add(t);
        
        
    }
Insert test;
}
VamsiVamsi
Hi,

I tested it for case and opp and don't find any issues. Please make sure that you added all the required fields for Test record in the above code.
 
The above code now creates test records for every update on payee irrespective of value changes made to payee and to be more accurate please add criteria to the above code as below 
 
trigger TestRecordInsert on Payee__c (Before Update) 
{
    List<Test__c> test = new List<Test__c>();

    For(Payee__c p:Trigger.old)
{
    if(p.Amount__c!=Trigger.newmap.get(p.id).Amount__c && p.Payee_amount__c!=Trigger.newmap.get(p.id).Payee_amount__c &&p.payee_name__c !=  Trigger.newmap.get(p.id). payee_name__c)
{
        Test__c t= new Test__c();
        t.Amount1__c = p.Amount__c;
        t.Payee_amount__c = p.Payee_amount__c;
        t.payee_name__c = p.payee_name__c;
        test.add(t);
}
  }
Insert test;
}