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
Galeeb SKGaleeb SK 

Display Old and New values

Hi,
Iam tried to display old and new values using trigger.if we update the values new values replace instead of old values.but i want to display both old and new values, how?for example if update Text A instead of  Text B so we display both Text A and Text B.Tell me with example.
Thank you
galeeb
Galeeb SKGaleeb SK
Hi
Sumit it not works it will display an error like  
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger displayOldAndNewValue caused an unexpected exception, contact your administrator: displayOldAndNewValue: execution of BeforeUpdate caused by: System.SObjectException: Invalid field skgaleeb786@gmail.com for Account: Trigger.displayOldAndNewValue: line 7, column 1

But our aim is to display old value and new value in some location or some where,but not concatnate
Thank you
galeeb
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Galeeb, 
You can try this - 

Here, I am concatenating the old value that we are getting from the "trigger.oldmap" with the new value.
trigger displayOldAndNewValue on Account (before update) {
    for(Account accRec : Trigger.new){
          // Replace "Testing__c" with your own field.
         accRec.Testing__c = Trigger.oldmap.get(accRec.id).Testing__c+ ' '+ accRec.Testing__c;		
    }
}
Hope, this will help you.

Thanks,
Sumit Kuamr Singh
 
suresh sanneboina 4suresh sanneboina 4
Hi,

You can Set History Tracking or create a new Field Like TestingOldValue__c

trigger displayOldAndNewValue on Account (before update) {
    for(Account accRec : Trigger.new){
        // Replace "Testing__c" with your own field.
        if(accRec.Testing__c != Trigger.oldmap.get(accRec.id).Testing__c)
        {
         //accRec.Testing__c = Trigger.oldmap.get(accRec.id).Testing__c+ ' '+ accRec.Testing__c;        
         accRec.TestingOldValue__c = (accRec.TestingOldValue__c == null?accRec.Testing__c:accRec.TestingOldValue__c + ','+accRec.Testing__c);
        }
    }
}

 
Sumit Kumar Singh 9Sumit Kumar Singh 9

Hello Galeeb,

It is working fine in my Dev Account. You might be missing something. Your question was not clear that you want to save it to some other location.

If this is the case - 

Create 2 fields - Let's say -  "Testing1" and "Testing2"

You can show "Testing1" on the Edit layout and "Testing2" on Detail layout.

Copy the value from "Testing1" to "Testing2" i.e "Testing2" will have the old and new values, while the "Testing1" will always have the last value.
You can also clear the "Testing1" field after copying the value to "testing2".

Thanks,
Sumit Kuamar Singh

Galeeb SKGaleeb SK
Hi Sumith Kumar Singh 9
Actually my question is to store the previous value and new value in some where like any field, etc
for example we have a field Text A,in that store a value 5,after update the value 10 instead of 5.so new value is 10,old value is 5.according to our scenario we have store both  old and new values in one place like old value is 5,new value is 10.i.e
Thanks
galeeb
suresh sanneboina 4suresh sanneboina 4
check the below code
trigger displayOldAndNewValue on Account (before update) {
    for(Account accRec : Trigger.new){
        // Replace "Testing__c" with your own field.
        if(accRec.Testing__c != Trigger.oldmap.get(accRec.id).Testing__c)
        {
         //accRec.Testing__c = Trigger.oldmap.get(accRec.id).Testing__c+ ' '+ accRec.Testing__c;        
         accRec.TestingOldValue__c = (accRec.TestingOldValue__c == null?'Old Value:'+accRec.Testing__c:'Old Value:'+Trigger.oldmap.get(accRec.id).Testing__c + ', New Value : '+accRec.Testing__c);
        }
    }
}
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Galeeb,

What if, the user edits the record third time? How would you want to show that?? OR is there any restriction that user can only edit the record only 2 times??

Thanks, 
Sumit Kumar Singh
Galeeb SKGaleeb SK
Hi Sumith,
No Restriction to edit the records.if any restriction then how to restrict, how to avoid the restriction.
Thanks
galeeb