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
María Jesús CarmonaMaría Jesús Carmona 

Old value of field on Apex test?

Hi,
I have an Apex class that compares on some methods the current and the old value of fields.
My question is, for example, If I have an Opportunity and change its name, how can I get the old name value from the test class to compare it with the new/current one?
Thanks.
Raj VakatiRaj Vakati
Try like this
 
Opportunity o = new Opportunity();
        o.name = 'Test';
        o.AccountId = a.Id;
        o.StageName = 'Closed Won';
        o.CloseDate = Date.today();
        o.Type = 'New Customers';
        
        insert o;
		
		String oldName = o.Name ; 
		
		o.Name = 'Udpdate' ; 
		
		update o; 
		
		String newName = o.Name ;