SOQL Query on History Objects

We can turn on history tracking on some standard and any custom object for specific fields. In case that field is changed, a history record is created, specifying the old value, and the new value, and who updated it.

SOQL can query these records as well:

Query on standard object’s history Object:

SELECT Field, OldValue , NewValue, CreatedBy.Name
FROM ContactHistory
WHERE ContactId = '003aertyulopuioQWE'

In the above query, all contact history records are fetched for contact whose id is ‘003aertyulopuioQWE’

Query on custom object’s history Object:

SELECT Field, OldValue , NewValue, Parent.Name
FROM Clinic__History
WHERE Parent.Name = 'Orlando'
Important
Note that for Contact the history object is ContactHistory, whereas for Clinc__c, it is Clinic__History.

History Object fields:

  • ParentId is the Id of the record.
  • Field is the name of the field.
  • OldValue is the old value of the field.
  • NewValue is the new value of the field.