I need to write a trigger that sends out an email notification whenever an account, lead, and contact is deleted. Also, what is the best test class to write in order to get 100% code coverage?
Here is the code:
trigger EmailAfterDelete on Account(after delete) {
Messaging.reserveSingleEmailCapacity(trigger.size);
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
for (Account acct : Trigger.old) {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new String[] {'[email protected]'});
email.setSubject('Deleted Account Alert');
email.setPlainTextBody('This message is to alert you that the account named ' + acct.Name + ' has been deleted.');
emails.add(email);
}
Messaging.sendEmail(emails);
}
Here is the error that I am getting every time I try to delete an account:
Validation Errors While Saving Record(s) There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger EmailAfterDelete caused an unexpected exception, contact your administrator: EmailAfterDelete: execution of AfterDelete caused by: System.NoAccessException: The organization is not permitted to send email: Trigger.EmailAfterDelete: line 2, column 1".