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
udayarangareddy mekalaudayarangareddy mekala 

Trigger3232

Hi
Please tell me how to compare the oldvalue fields to the newvalue fields in the trigger testclass


Thanks
rangareddy
AshlekhAshlekh
Hi,

In test class you can trigger the code of trigger through DML action.

And in Trigger code you can get the value of Trigger.Old and Trigger.new values of the reord.

Trigger.old is null in the cases for eg. in insert opperation.
 
trigger test on Account(before update)
{
​  for(Account a : trigger.new)
  {
	Account oldacc = Trigger.oldMap.get(a.id);
	if(oldacc.name == a.name)
	{
		//Write code 
	}
  }
}
Test class
 
@isTest
private class TESTAccountTriger {
	
	static testMethod void firstMethod()
	{
		Test.startTest();
		Account a = new Account(name= 'Test');
		insert a;
		a.name = 'test change';
		a update;
		Test.stopTest();
	}

}


-Thanks
Ashlekh Gera
 
Arun Deepan LJArun Deepan LJ
Hi,

Generally the old values can be obtained in the trigger using either Trigger.OldMap or Trigger.Old. All the content in those list are Old values, where as, the content of the Trigger.NewMap and trigger.New are New values. 
Remember, Trigger.Old values will be same for the entire execution and you can easily differentiate old values from the new values as shown in above example