How to query Field History Tracking records in Salesforce?

How to query Field History Tracking records in Salesforce?

If field history tracking is enabled for an object, the changes are stored in history table for that particular object.

The API name of these tables follows a simple convention so should be easy to find. For standard objects, the name of the history table follows the format: ‘ObjectNameHistory’ so for the Account object the history table is AccountHistory.

For custom objects, the name of the convention simply replaces the ‘c’ on the end of the API name with ‘History’. So, for a custom object call My_Custom_Object__c the history table is called My_Custom_Object__History.

Sample SOQL Queries:

Standard Object:

SELECT AccountId, OldValue, NewValue, IsDeleted, Id, Field, CreatedBy.Name 
FROM AccountHistory 
WHERE AccountId = '<AccountId>' 
ORDER BY CreatedDate DESC

Custom Object:

SELECT OldValue, NewValue 
FROM My_Custom_Object__History
WHERE ParentId = '<CustomObjectRecordId>'
ORDER BY CreatedDate DESC

Leave a Reply