Apex Triggers for Email Messages

by Quinton Wall on October 30, 2009 at 10:41 AM

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?

TrackBack

TrackBack URL for this entry: http://www.typepad.com/services/trackback/6a00d8341cded353ef0120a6935540970c

Listed below are links to weblogs that reference Apex Triggers for Email Messages:

Comments

Posted by Marco Casalaina on October 30, 2009 11:46 AM:

Note that at the time of this writing the EmailMessage object only pertains to emails that are associated with the Case object.

Posted by amir hafeez on November 2, 2009 01:57 AM:

Your getAccountByCEOEmail method does that do an soql search, if so you have that in your for loop - governor limit?

Posted by Quinton Wall on November 2, 2009 02:10 PM:

Hi Amir,

You are correct. It is best practice to not put SOQL statements within for loops. In the simple example I posted you could certainly refactor the method call to operate on a collection of emails, but for demonstrates sake I was 'hardcoding' the email address so the method would have only been called once.

But you correct, and great job point this out.

Posted by Sid Kabe on December 14, 2009 03:59 AM:

hey this is a nice job done,
just yesterday I had a nightmare that I might be falling behind the Salesforce.com updates.

But thanks to you now I am testing new features.

Cheers,
SiD
http://force.siddheshkabe.co.in

Post a comment

If you have a TypeKey or TypePad account, please Sign In