When im running this
Select Account.name, NewValue, OldValue, IsDeleted from AccountHistory where Field = 'Owner' ORDER BY CreatedDate DESC
in salesforce it is giving me two records for one change.
added based on comments
If you need to get the history without the Id
s, then you can use below code snippet:
List<AccountHistory> ach = [SELECT Id, Account.Name, Field, OldValue, NewValue
FROM AccountHistory
WHERE Field = 'Owner'
ORDER BY CreatedDate DESC];
List<AccountHistory> targetAch = new List<AccountHistory>();
for(AccountHistory ah : ach)
if(!(ah.NewValue instanceof Id))
targetAch.add(ah);
targetAch
will have List<AccountHistory>
without Id
s. If you need Ids only, then you can flip the If
condition by removing !
.
Old answer
The reference fields like Owner
, Parent
etc will be tracked for both Id
and Name
changes. So, for every change in record for these fields, 2 corresponding history records are created. Notice that even for custom lookup Test_Object__c
, there will be 2 records created which references (1) Its Id
and (2) Its Name
PS: I remember seeing this documentation sometime back but right now unable to find it, pls feel free to add the link here if someone finds it.