I'm playing around in my Dev org and am using a trigger to send a basic e-mail to System Admins when an account is created as follows:
private static void doNotifyAdmin(List<Account> acc) {
Id p = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id;
List<String> toAddresses = new List<String>();
for (User u : [SELECT Email FROM User WHERE ProfileId = :p]) {
toAddresses.add(u.Email);
}
for (Account a : acc) {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject('New Account Created!');
email.setToAddresses(toAddresses);
email.setHTMLBody('The account: ' + a.Name + ' was added to the system!');
Messaging.SendEmailResult[] r = Messaging.sendEmail(
new Messaging.SingleEmailMessage[] {
email
}
);
}
}
This all works fine, but I was wondering whether or not I'm doing it to best practice, particularly in the for
loop sending the SingleEmailMessage
.
In debug logs I noticed the following line:
Number of Email Invocations: 1 out of 10
As I'm the only account in the dev org, this isn't a big deal but if I were to send e-mails to 20 people in this way, for instance, would this fail? Or would it be fine given I'm querying addresses to send to in the toAddresses
list?
Just curious to know if I'm engaging in best practices here!
Thanks!