As soon as a Case with Priority ‘High’ is created, an email alert should be sent to support agents or managers, so they’re instantly notified.
Business Logic:
- When a Case is inserted,
- Check if
Priority == 'High'
, - Send an email alert with case details (Subject, Case Number, etc.)
Where do we use this?
There are 2 options:
- Flow (recommended for simple alerts — click-based)
- Apex Trigger with
Messaging.SingleEmailMessage
(for more control)
Apex Trigger Code:
trigger HighPriorityCaseAlert on Case (after insert) {
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
for (Case c : Trigger.new) {
if (c.Priority == 'High') {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new String[] {'support@sfdctelugu.in'});
email.setSubject('🔥 High Priority Case Alert: ' + c.CaseNumber);
email.setPlainTextBody(
'A high-priority case has been created:\n\n' +
'Subject: ' + c.Subject + '\n' +
'Case Number: ' + c.CaseNumber + '\n' +
'Description: ' + c.Description
);
emails.add(email);
}
}
if (!emails.isEmpty()) {
Messaging.sendEmail(emails);
}
}
What You Need:
- A valid org-wide email address setup.
- Replace
support@sfdctelugu.in
with a valid internal email. - You could also fetch the user/group dynamically if needed.
Test Class:
@isTest
public class HighPriorityCaseAlertTest {
@isTest
static void testHighPriorityCaseSendsEmail() {
// No need to test email delivery — just test no error on insert
Case c = new Case(Subject = 'Urgent Issue', Priority = 'High', Status = 'New');
insert c;
}
@isTest
static void testLowPriorityCaseDoesNotSendEmail() {
Case c = new Case(Subject = 'Minor Issue', Priority = 'Low', Status = 'New');
insert c;
}
}
Anonymous Window Test:
Case c = new Case(Subject = 'Server Down', Priority = 'High', Status = 'New');
insert c;
If your org email settings are correct, the recipient will get an alert instantly.