We have a trigger on before insert of an EmailMessage to check the contacts in the from, to and cc and based on that, determine if the mail message should be visible to externals.

The code itself always runs fine and is pretty simple and gives the proper yes / no value to update the IsExternallyVisible flag but sometimes, it seems that the code just isn't called... even though, it's just a simple "before insert" without filtering out any records...

Running the same code again afterwards for those records that got skipped by the trigger then correctly sets the IsExternallyVisible flag...

Might there be situations where the "before insert" of an EmailMessage isn't executed???


2 Answers 2


Like I thought, it didn't have anything to do with the code as such but the issue was as follows:

Salesforce itself also has it's logic to determine if the "IsExternallyVisible" flag should be true or false. You can find an explanation of their logic here:


What is important to know is that the code of Salesforce for the IsExternallyVisible flag executes between the "before insert" and "after insert" event.

We didn't know that at first and logically, our logic to set the IsExternallyVisible field was being launched in the "before insert" handler.

So, in the end, causing the Salesforce logic around the IsExternallyVisible to be the boss instead our own custom logic...

So, how we will fix it is either:

  • executing the same logic but in the after insert. (meaning we will have to re-query the records cause those from the trigger context would be read only)
  • or, executing the logic as a future method

In my eyes, this is very strange behaviour of Salesforce and in any case not handy. They should at least put this a bit more clear in the documentation. Will definitely log a case for this.

UPDATE: I assume the logic will also have to be executed in the "after update" for the case where someone would have first saved an email message as a draft...

UPDATE 2: it was not possible to execute the code to set the IsExternallyVisible flag in the after insert and I had to do it with a future method. When trying to update the email message in the after insert, I got something like method not allowed, or some permission error. I remember getting this when the email message you were trying to update also contained other fields apart from the "IsExternallyVisible" flag but this wasn't the case now. Doing exactly the same in a future call did the trick.


Sharing your code would make it much easier to determine where the issue might be occurring. Also to clarify, is it truly not firing, or is that an assumption based on the expected end result not occurring?

Without seeing your trigger, my first guess would be that you're using targetObjectId in setting the contact the email is being sent to, and if any of these emails are being triggered "simultaneously" when a contact is inserted, it's possible that the trigger is trying to use the id of a contact before that id exists. As a rule of thumb, if a trigger isn't saving information on the object it's linked with, it's best to make it an after trigger. Again, I don't know all the circumstances, or what all your trigger does, but without seeing it, that's my two cents.

  • hey, I'm not creating any contacts or so. I'm just setting the "IsExternallyVisible" flag based on information from the FromAddress, ToAddress / CcAddress . It's a pretty strong assumption that the code is just not executing and that the issue isn't with the code itself as when afterwards I query for those EmailMessage records with an incorrect "IsExternallyVisible" flag and run the same code again, it fixes the flag to have the correct value.
    – KoenVM
    Nov 19, 2015 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.