I'm trying to figure out a way to get last inserted records according to distinct lookup field. Let's say I have data like this;
Field ----- Lookup ----- CreatedDate
Data1 ----- User1 ----- Inserted 5 min ago
Data2 ----- User2 ----- Inserted 6 min ago
Data3 ----- User2 ----- Inserted 7 min ago
Data4 ----- User1 ----- Inserted 8 min ago
Data5 ----- User3 ----- Inserted 10 min ago
And I have distinct Lookup field Set like {User1,User2,User3}
I want to get data that highlighted with bold. Is it possible? Because getting data ordered by CreatedDate
does not return what I need.
UPDATE: I know that I can grab records ordered by CreatedDate then inside a for
I can get latest values for distinct fields. But I have a large data set. Because of this I need to avoid limits. But limiting to query, let's say 1000, might not always return record for every distinct lookup field. For User200 last inserted record might be 20 days ago. Then by limiting query to 1000 I might not get the record for User200.
Select FieldName from ObjectName where Lookup in: distinctUserList OrderBy Createddate desc
order by createddate
.Then by simply iterating it and checking the condition you can grab what you want.