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
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
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]