User Story
I'm a User
WHEN I update any Account and set the Status as Active
THEN the Person should be notified with an Email
Code
trigger SendActivationEmail on Account (after update) {
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
for(Account acc : Trigger.new) {
if(acc.Status__c == 'Active' && acc.Status__c != Trigger.oldMap.get(acc.Id).Status__c) {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new String[] { acc.Email__c });
email.setSubject('Your account is now active');
email.setPlainTextBody('Congratulations! Your account is now active.');
emails.add(email);
}
}
if(!emails.isEmpty()) {
Messaging.sendEmail(emails);
}
}