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
LinThawLinThaw 

How to get event records of Max date for each Account?

Hi there,

We need to get one of event record fields including(Id, AccountId, customField1, StartDateTime) for each Account, which startDateTime is max.

Is possible to get as following result?

User-added image
User-added image
Thanks & Regards,
LinThaw
Raj VakatiRaj Vakati
Use this Query 
 
SELECT Count(Id), Account.Name, WhoId, MAX(StartDateTime)
FROM Event
GROUP BY WhoId ,Account.Name

 
LinThawLinThaw
Thanks Raj,

I want [customField1] field too, is possible?
I think we must use order by and make some logic in apex,
to get other fields we want.

Regards,
LinThaw
Raj VakatiRaj Vakati
SELECT Count(Id), Account.Name,FSL__Event_Type__c, WhoId, MAX(StartDateTime)
FROM Event
GROUP BY WhoId ,Account.Name,FSL__Event_Type__c

 
LinThawLinThaw
Hi Raj,

Thanks for your quick reply.

If we use [customField1] in Group By,
we don't get one exact record for Account. 

Regards,
LinThaw