I am setting up a trigger for when email messages are after inserted and we use email-to-case.

Is there any way that the email-to-case inbound or normal sf outbound message system could result in two emails being inserted at the same time so that two or more email messages records would be sent to the trigger?

If a customer emails us rapidly, double-clicked send, and their client fired out two emails within milliseconds or seconds of each other, would SFs inbound mail servers aggregate that and try to insert both records at the same time?

2 Answers 2


Your triggers should always assume that records may be processed in bulk. The EmailMessage object could conceptually have triggers that fire more than one message at once, such as sending emails through Apex. The documentation itself does not specify if bulk behavior is possible with email-to-case, so you should simply assume that it is possible to avoid mishandling records. It's better to spend a few extra lines of code for a situation that may never happen, than to try and "save" those lines of code only to break business logic. Note that this is a bit opposite of my usual advice, which is to avoid writing code that will absolutely never be called (e.g. a try-catch around a block of code that can't throw exceptions). Always bulkify triggers, flows, etc. You never know when you'll need it.

  • 1
    Thanks, I felt that was the "right" thing to do, but wanted to avoid convoluting things with a map if not necessary. Added my solution and use case as an approach for others below.
    – S.B.
    Apr 19, 2020 at 19:38

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);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .