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!).
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;
}
}
}
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