User Story
As a user, when I create a new case with high priority, I want to receive an email notification so that I can quickly respond to the issue.
Code
trigger CaseTrigger on Case (after insert) {
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
for (Case c : Trigger.new) {
if (c.Priority == 'High') {
User owner = [SELECT Email FROM User WHERE Id = :c.OwnerId];
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new List<String>{owner.Email});
email.setSubject('New High Priority Case Created');
email.setPlainTextBody('A new high priority case has been created: ' + c.Subject);
emails.add(email);
}
}
if (!emails.isEmpty()) {
Messaging.sendEmail(emails);
}
}