function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Getting last change to a specific field record set

I have Object X with field foo. I have field history tracking turned on and I am tracking history of foo__c.


I would like to get a record set which has the LAST CHANGE to foo across all instances of X - i.e (XId, LastFooValue, LastUpdateTime,LastUpdatedBy>. . Is there a way of doing it using SOQL WITHOUT writing any code? Note that I need this query for data loader export, which has it's limitations as mentioned below.


X_History[] hist = [select Parent.Name, NewValue,CreatedDate,CreatedBy.Name from X_History where Field = 'foo__c'] => if there are 3 changes for a specific record, I get three entries. The history table supports GROUP BY  ParentId, but not NewValue. Even if it did, I can't get the latest <NewValue,CreatedDate> using Max function. I could get Max(CreatedDate), but not the NewValue asssociated with Max(CreatedDate)

X__c[] Xhist = [select Name, (select NewValue,CreatedDate CreatedBy.Name from Histories where Field = 'foo__c' ORDER By CreatedDate DESC Limit 1) from X__c] => returns all X records, including those  that have NO change to foo.  Besides, I need this for dataloader export. Nested queries NOT supported by dataloader export.