I ran into an issue the other day where I wanted to send out an email notification as part of a trigger. The code itself was no problem but, like most days, my problems began as I tried to write the unit test. I couldn't get the required test coverage on those lines of code.
I contacted our support development rep and he suggested moving the mail functions into their own class and then pass the required parameters to send the mail. This is what I came up with:
public class MailerUtils {
public static void sendMail(string message) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'me@email1.com','you@email2.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('My Subject');
mail.setUseSignature(false);
mail.setHtmlBody(message);
// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
public static testMethod void testSendMail() {
sendMail('This is my email message');
}
}
Here is a trigger that would fire after insert and send an email:
trigger TestEmail on Contact (after insert) {
for (Contact c : Trigger.new) {
MailerUtils.sendMail('Welcome ' + c.Name);
}
}