In Salesforce, we have a feature that allows us to track the changes made to fields through field history tracking. We can retrieve the details of the field in Salesforce Relationship Queries in SOQL to obtain the details of changes in the object’s field values.
In this Salesforce tutorial, I will explain how we can track object field history in Salesforce using SOQL queries.
SOQL Query to Track Object Field History in Salesforce
In Salesforce field history tracking, we monitor two values: the old field value and the updated field value.
In the example below, we will retrieve the field history details of the account fields using the SOQL query, which will return the old value, the new value, and the field in which changes have been made.
SOQL query:
SELECT Id, OldValue, NewValue, Field FROM AccountHistoryOutput:

This way, we can track the object field history using SOQL queries in Salesforce.
SOQL Query to Track Field History of Specific Object Records in Salesforce
When you have to query the field history details of a specific object record, the SOQL query will be executed with the ID of that particular record.
SOQL query:
SELECT AccountId, OldValue, NewValue, IsDeleted, Id, Field, CreatedBy.Name
FROM AccountHistory
WHERE AccountId = '001J3000007zBCKIA2' Output:

This way, we can query the field history details of any specific object record in Salesforce using SOQL.
SOQL Query to Track Custom Object Field History in Salesforce
In Salesforce, we can also track the field history of the custom object using the SOQL query. First, ensure you have set up field history tracking for that custom object.
For example, you have a custom object, ‘Employee_c,’ and you have to query the custom field, ‘Email_c,’ so you can execute the SOQL query in the following way.
SOQL query:
SELECT Field, OldValue, NewValue
FROM Employee__History
WHERE Field = 'Email__c'Output:

Executing the SOQL query with the above method, you can track the custom object history through SOQL. Here, make sure to enter the object name as ‘customObject__History.’
SOQL Query to Get SObject Fields with Field History Tracked
This SOQL query retrieves the Salesforce object fields with field history tracking enabled. This query is valid in scenarios such as displaying all Salesforce object fields with field history tracking enabled in a dropdown menu within a Lightning web component.
In the SOQL query below, we will find the fields that are enabled for field history tracking in the Account object.
SOQL query:
SELECT Label, QualifiedApiName
FROM FieldDefinition
WHERE
EntityDefinition.QualifiedApiName = 'Account'
AND IsFieldHistoryTracked = TRUEOutput:

In the output, we will see the label and Api name of the object fields on which we have enabled the field history tracking.
This way, we can query and track the details of field history tracking in Salesforce for the various scenarios and conditions discussed in the above examples.
Conclusion
In Salesforce, by querying the field history using SOQL, we can monitor changes made to specific fields in standard and custom objects. By retrieving field tracking results through queries, we can efficiently analyze the modified data in the organization and keep track of activities related to the changes made to the object field values.
By following the method in the above example, you can efficiently query the field history tracking details in Salesforce.
You may also like to read:
- For Clause in SOQL
- Get User Or Logged-In User Details Using SOQL
- OFFSET Clause in SOQL
- SOQL Query on Group and GroupMember in Salesforce
- Query to Count Child Records in Salesforce SOQL
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.