0

when i execute this:

Account acc = new Account(Name = 'newTest1',Phone = '+4444444444');
insert acc;
acc.Name = 'ChangedTestAnonimusAcc';
acc.Phone = '+55555555555';
update acc;
List<FeedItem>  feedItems = [SELECT ID,Body,ParentId,Title,(SELECT ID, FieldName, OldValue, NewValue FROM FeedTrackedChanges ORDER BY ID DESC)
 FROM FeedItem WHERE Type='TrackedChange' AND ParentId =: acc.Id];
System.debug(feedItems);

feedItems are empty(but I know FeedItem with type TrackedChange was created).I couldn't understand why.

3
  • Can you remove ParentId =: acc.Id in the where condition and see what you get? May 3, 2020 at 9:39
  • I get many different FeedItem
    – Noker
    May 3, 2020 at 9:45
  • the problem is that when I take an ID of ChangedTestAnonimusAcc which already created and making a request : [SELECT ID,Body,ParentId,Title,(SELECT ID, FieldName, OldValue, NewValue FROM FeedTrackedChanges ORDER BY ID DESC) FROM FeedItem WHERE Type='TrackedChange' AND ParentId = ' string with ChangedTestAnonimusAcc Id'] . I get all changes (Name and Phone );
    – Noker
    May 3, 2020 at 9:54

1 Answer 1

4

The history tracking records are created asynchronously. You can confirm this behaviour by simply running the update on an account and comparing the created date time of tracked change with Account last modified date and also simply because the apex log has no mention of the history record being created.

This is the reason why the tracked changes are not available in the same transaction but appear afterwards when you query.

5
  • is it possible to wait for the creation and react to it? (like setcallback in lightning).
    – Noker
    May 3, 2020 at 12:45
  • 1
    Yes, but that would depend if salesforce is able to create history records by that time or not and wouldn't be definitive.
    – manjit5190
    May 3, 2020 at 12:54
  • Could you then please suggest what is better to use for this purpose(wait when FeedItem with type TrackedChange created and react to it)
    – Noker
    May 3, 2020 at 13:38
  • That would depend the use case. What is the purpose to query the tracked records?
    – manjit5190
    May 3, 2020 at 13:49
  • I'm writing a test class for a method that copies all TrackedChange records from one object to another by id.That is for the test I need to create a new account from the code with Feeditem type Trackedchange and finally check how the data was copied from FeedItem of one object to another object.
    – Noker
    May 3, 2020 at 14:02

You must log in to answer this question.

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