Fields

SOQL Field History

Querying Field History

SOQL field history queries track field changes with History objects.

Introduction to SOQL Field History

SOQL (Salesforce Object Query Language) field history allows you to track changes made to specific fields within Salesforce objects. This is particularly useful for auditing purposes and understanding how data evolves over time. The changes are stored in History objects, which are associated with the object whose field history you want to track.

Understanding History Objects

History objects in Salesforce are special types of objects that store historical data for fields tracked on a standard or custom object. Each standard or custom object in Salesforce can have an associated History object. These objects typically have the suffix __History (e.g., AccountHistory, OpportunityHistory).

Structure of a History Object

History objects have several important fields to note:

  • Field: The field that was changed.
  • OldValue: The value of the field before the change.
  • NewValue: The value of the field after the change.
  • CreatedById: The ID of the user who made the change.
  • CreatedDate: The date and time when the change was made.

Querying History Objects with SOQL

SOQL provides the ability to query History objects to retrieve field change data. You can use these queries to filter changes by specific fields, users, or date ranges. Below is a basic query example to fetch changes from an Account object.

Filtering by Field Changes

To filter your query to track changes to a specific field, you can add a WHERE clause with the Field criteria. Here's how you can filter changes to the Industry field of an Account:

Practical Use Cases for Field History Queries

Field history tracking can be used in various scenarios, such as:

  • Auditing data changes for compliance purposes.
  • Tracking user activity and changes over time.
  • Identifying trends in data modifications.
  • Providing historical context for data-driven decision making.