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
sfAdmin BanjosfAdmin Banjo 

How do I test LeadHistory table? Geting empty query result.

Desperately needing some help here! 

I have a batch class that sends an email to a lead owner based on the Status not changing during a specified timeframe. I manually tested it (without the time query filter) and the code was doing what it needs to do. I've now written the test code only to find the Leadhistory query returns an null record set. I can see the Leads have an ID and I perform some status updates on the test method to generate the LeadHistory records so im clueless as to why it is not returning any records.

test method:
private class LeadStatusBatchTest {
	@isTest static void test_method_one() {
		// First Load some Leads from resource file
		List<sObject> leads = Test.loadData(Lead.sObjectType, 'Default_Lead_Test_Data');
		system.debug('### [LeadStatusBatchTest] leads (loaded test data): '+ leads);

		//set a datetime to 4 weeks ago to update createdDate
		Datetime weeksAgo_4 =;
		for(Lead ld: (List<Lead>)leads) {
			//update the status and save to DB
			ld.Status = 'Open';
			Test.setCreatedDate(ld.Id, weeksAgo_4.addDays(-7));
		update leads; //to generate a leadHistory entry for Open status
		system.debug('### [LeadStatusBatchTest] Updated CreatedDate on Leads: '+ [Select id, CreatedDate From Lead where Id = :leads]);

		//validate update worked
		for(lead ld: [select id, status, createdDate From Lead where id in : leads]){
			system.assertEquals('Open', ld.Status);
			system.assertEquals(weeksAgo_4.addDays(-7), ld.CreatedDate);

		//get the leadhistory records and update their createddate to same as lead created date
		for(LeadHistory lh: [select id, oldvalue, newvalue from LeadHistory where LeadId =: leads]){
			Test.setCreatedDate(, weeksAgo_4.addDays(-7));
		system.debug('### [LeadStatusBatchTest] LeadHistory testdata: '+ [select id, oldvalue, newvalue from LeadHistory where LeadId =: leads]);

		//update Status to Contacted
		for(lead ld: (List<Lead>)leads){
			ld.Status = 'Contacted';
		update leads;
		//get LeadHistory for Lead and update the CreatedDate
		for(LeadHistory lh: [select id, oldvalue, newvalue from LeadHistory where LeadId =: leads]){
			if(lh.newvalue == 'Contacted')
				Test.setCreatedDate(, weeksAgo_4);
		system.debug('### [LeadStatusBatchTest] leadHistory Contacted status: '+ [select id, oldvalue, newvalue from LeadHistory where LeadId =: leads]);

		//validate LeadHistory CreatedDate
		for(LeadHistory lh: [select id, CreatedDate from LeadHistory where LeadId =: leads]){
			system.assertEquals(weeksAgo_4, lh.CreatedDate);

			//run batch class
			LeadStatusBatch db = new LeadStatusBatch();
		system.debug('### [LeadStatusBatchTest] Stopped Test. ');
		//make validations if execution was successful


When I run the test the test method debugs return a null data set on the LeadHistory query that is supposed to set the CreatedDate. All queries before that work just fine.

Is it that you cant query hte LeadHistory table on a testmethods? (doesnt seem right)
Or that changes to the lead are not tracked in LeadHistory?

Any help here would be much appreciated! 
Add (SeeAllData=true) to your @isTest declaration.
Lorant DobrondiLorant Dobrondi


Did you manage to achieve this?

sfAdmin BanjosfAdmin Banjo
No in the end the requirements evolved again and it was delivered via a couple of reports, so in reality I never fixed this issue. Mauricio Ramos Senior SFDC Developer [cid:image006.png@01D0600B.85A9AA20] M: +61 401 096 334 E: W: [SFU_CRT_BDG_Pltfrm_Dev_I_RGB] [SFU_CRT_BDG_Pltfrm_App_Blder_RGB] [SFU_CRT_BDG_Force_com_Devlpr_RGB]