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
Jeremy York 7Jeremy York 7 

Help with a query on Email Message object

I need help creating a query that will retrieve(email message record id) from email message object only the oldest records(by created date) per case record.  I need to archive and purge any email messages on cases with leaving just the most recent email message record on the case. 
Vishal_GuptaVishal_Gupta
Hi Jeremy,

you can use the following query :

select id, Contact.Id, SuppliedEmail, (select id, ccaddress, BccAddress, FromAddress, Subject, ToAddress from EmailMessages) from case

Please let me know if I can help you more on it.

Thanks,
Vishal
Vishal_GuptaVishal_Gupta
Hi,

We can put order by to get latest Email, like below query :

select id, Contact.Id, SuppliedEmail, (select id, ccaddress, BccAddress, FromAddress, Subject, ToAddress from EmailMessages order by CreatedDate ASC) from case

Thanks,
Vishal
 
Jeremy York 7Jeremy York 7
Thanks Vishal, that gets me close, but I need to pull the results into Excel to remove the ones I do not want to delete. Since it is a nested query it seems difficult to find an editor that allowes exporting it to csv or excel.