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

Lead History Table difference between Production / Sandbox

Hi Support / PM,


I am working with the LeadHistory table at same place in my code,

to find the previous owner of a Lead.


The funny thing (and the reason of this post) is, that in my production the "oldValue" field contains/returns the ID of the previous owner and in my Sandbox it returns the Name.


I didn't changed the API-Version, so this should not be the problem.


This is my query:



		LeadHistory leadHistory = [
			SELECT Field, LeadId, OldValue, CreatedDate 
			FROM LeadHistory 
			WHERE LeadId = :leadId 
				AND Field = 'Owner'
			ORDER BY CreatedDate DESC
			LIMIT 1



and this is my Debug output:



System.debug('oldValue content: ' + leadHistory.OldValue);



Again: For production it returns ID, for Sandbox it returns Name.


It's not a problem for my app, as I quickly fixed it, but I wonder how this can differ between two orgs.


You can run the code from anonymous by putting a lead ID in the code. Of course you need field tracking on Lead Owner and at least one Owner change.







P.S. We still have no LeadHistory in Apex Tests...


Looking into the LeadHistory table is very strange. For each History entry it creates two entries in the table.

One contains the names and the other entry contains the IDs.


How should I make sure to get only the rows which have a User IDs in "NewValue"?

Add a WHERE to make sure "NewValue" starts with '0052'!? ... Sounds like a dirty workaround ;)

(EDIT: Would not even work, because: "field 'OldValue' can not be filtered in query call")


Thankful for any help.


Here's a screenshot of the query + result:



I was looking for Apex code coverage for a trigger which has references to LeadHistory table(like querying etc) and saw your note "We still have no LeadHistory in Apex Tests...". So what you are saying is we cannot write test scripts if we have used LeadHistory object ? Am i getting it right ? Please let me know. Also is there any place/doc where salesforce confirmed this ?