My use case was for updating a flag on emails to identify the last received email under a parent case, I accomplished handling the possibility of bulk records with a map of Id, EmailMessage:
public class emailMessageTriggerHandler {
public void postInsertEmailProcess(List<EmailMessage> listOfEmailMessages) {
Map<Id,List<EmailMessage>> mapOfRelatedEmailsToUpdate = new Map<Id,List<EmailMessage>>();
for (integer i=0; i < listOfEmailMessages.size(); i++) {
if (listOfEmailMessages[i].ParentId != null) {
mapOfRelatedEmailsToUpdate.put(listOfEmailMessages[i].ParentId, new List<EmailMessage>());
}
}
List<EmailMessage> listOfRelatedEmailsToUpdate = [SELECT Id, ParentId, Last_Email_for_Case__c FROM EmailMessage WHERE ParentId = :mapOfRelatedEmailsToUpdate.keySet() ORDER BY CreatedDate DESC];
for (integer i=0; i< listOfRelatedEmailsToUpdate.size(); i++) {
List<EmailMessage> relatedListOfEmails = new List<EmailMessage>();
relatedListOfEmails = mapOfRelatedEmailsToUpdate.get(listOfRelatedEmailsToUpdate[i].ParentId);
relatedListOfEmails.add(listOfRelatedEmailsToUpdate[i]);
mapOfRelatedEmailsToUpdate.put(listOfRelatedEmailsToUpdate[i].ParentId,relatedListOfEmails);
}
}
}