Apex Triggers for Email Messages | Salesforce Developers Blog

One of the things about working for a company that innovates at such an incredible pace is that you are constantly tasked with keeping current on the latest release. Last night, as I curled up into bed, I did what any good technologist would do: I popped open my laptop, and brought up the pdf containing the Winter '10 release notes (I'm trying to save trees after all!).

Reading, and keeping the last few release notes readily accessible is a good habit I have gotten into at Salesforce.com. It is a great way to find those features which may not have highlighted as part of the release. One such feature is the ability to set a trigger based on Email Message received. (You can also now set a trigger on Case Comments as well — but that's a topic for another blog)

Take a really silly example (but probably quite useful!) where Account Executives want to be notified via a Task in the app whenever an email is received from a CEO of a company that is on their watchlist. We could leverage the new EmailMessage trigger functionality to automate this:

trigger alertAEonCEOEmails on EmailMessage (after insert) {

for(EmailMessage message : trigger.new)


//blogware – if this was real, we wouldn't hardcode the email here

if(message.FromAddress == 'ceo@mybigdealthisquarter.com')


    Account theAccountSO = getAccountByCEOEmail(message.FromAddress);

    Task callTheCEO = new Task();

    callTheCEO.OwnerId = theAccountSO.OwnerId;

            callTheCEO.Subject = 'CEO Emailed';

    callTheCEO.Description = 'You Better call him back;

    callTheCEO.ReminderDateTime = System.now();

    callTheCEO.AccountId = theAccountSO.id;




The ability of set triggers on Email Messages opens up many possibilities. My simple example is only one; what are yours?

Stay up to date with the latest news from the Salesforce Developers Blog