I want to track history(History which is shown below every record in salesforce user interface) of a record in apex class. How can i do this? What will be used in SOQL query?


If you have field history tracking enabled for an object then the changes are stored in a special history table for that particular object.

The naming 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

It's worth having a look through the metadata for your org using something like workbench so that you can see the history tables and the fields that they contain.


History is available on History Enabled Objects as child Object and its relationship name is Histories. You can query this child object by inner SOQL query like this:

SELECT Id, Name, (Select OldValue, NewValue From Histories) FROM My_Custom_Object__c

However you cannot do:

[SELECT id FROM History]

  • You can query each object's History table directly, have a look at @SMGoodyear 's answer. Sep 23 '13 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.