0

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.

Here is the screenshot for the same

1 Answer 1

1

added based on comments

If you need to get the history without the Ids, 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 Ids. 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

enter image description here

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.

5
  • Okay. How do we get the single record ? I need this account data (without duplicate) for other logic to be processed further, any filter we need to consider here ?.
    – RaJu
    Commented Aug 28, 2019 at 6:58
  • do you need only Id values? Commented Aug 28, 2019 at 6:59
  • No, I need the other values without Ids, anything works fine but i need only one from those two
    – RaJu
    Commented Aug 28, 2019 at 7:51
  • added in answer Commented Aug 28, 2019 at 7:59
  • @RaJu is your issue solved? Commented Aug 28, 2019 at 16:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .