Track Object Field History in Salesforce Using SOQL

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 AccountHistory

Output:

SOQL query to track object field history

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:

SOQL query to track field history of specific object in Salesforce

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:

SOQL query to track field history of custom object in Salesforce

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 = TRUE

Output:

SOQL query to track fields with enabled field history in Salesforce

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:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.