- Published on
Effortless Automation: Email Notifications for Salesforce Cases via Email-to-Case, Flow, and Email Templates
- Authors
- Name
- Himanshu Varshney
Senior Salesforce Developer
Create an Email Template:
In Salesforce, navigate to Setup. In the Quick Find box, enter "Classic Email Templates" or "Lightning Email Templates" based on your Salesforce version. Create a new Email Template or use an existing one that suits your notification requirements.
Create a Flow:
Go to Setup.
In the Quick Find box, enter "Flows" and select "Flows."
Create a new Flow by clicking on "New Flow."
Use the Flow Designer to build your flow. Here's a simplified example of a flow that sends an email notification:
+---------------------+
| Start |
| (Record Trigger) |
+---------------------+
|
V
+---------------------+
| Record Lookup |
| (Lookup Case) |
+---------------------+
|
V
+---------------------+
| Decision |
| (Is Case Found?) |
+---------------------+
| |
| Yes | No
| |
V V
+---------------------+ +-----------------------+
| Send Email |--------| Create Case Record |
| (Use Email Template)| | |
+---------------------+ +-----------------------+
The Flow should start with a Record Trigger for the Case object.
Add a Record Lookup to check if the case already exists (using criteria like Email Subject, etc.).
Use a Decision element to check if the case was found.
If the case is found, proceed to the Send Email element, using the Email Template you created.
Activate the Flow:
Once you've created and tested your Flow, activate it. Configure Email-to-Case:
In Salesforce, navigate to Setup. In the Quick Find box, enter "Email-to-Case." Configure your Email-to-Case settings to use the Flow you created for the "On Create" trigger.
Test:
Create a test case by sending an email to your designated Email-to-Case email address. Ensure that the email creates a new case and triggers the Flow. Here's an example code snippet for sending an email in the Flow. Note that the actual code may vary based on your specific requirements and the complexity of your email template:
// Apex Class for sending an email in Flow
public class FlowEmailNotification {
// Method to send email using the specified email template
public static void sendEmailNotification(Id caseId, Id emailTemplateId) {
// Load the case record
Case myCase = [SELECT Id, Subject, Description, ... FROM Case WHERE Id = :caseId LIMIT 1];
// Load the email template
EmailTemplate emailTemplate = [SELECT Id, Subject, HtmlValue, Body FROM EmailTemplate WHERE Id = :emailTemplateId LIMIT 1];
// Create an Email Message
Messaging.SingleEmailMessage emailMessage = new Messaging.SingleEmailMessage();
emailMessage.setSubject(emailTemplate.Subject);
emailMessage.setHtmlBody(emailTemplate.HtmlValue);
emailMessage.setPlainTextBody(emailTemplate.Body);
emailMessage.setToAddresses(new String[] { 'recipient@example.com' }); // Replace with the recipient's email address
// Attach the case information to the email
emailMessage.setWhatId(myCase.Id);
// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { emailMessage });
}
}
In your Flow, you would use an "Apex Action" to invoke this Apex class and pass the necessary parameters such as the caseId and emailTemplateId.
Remember to adapt the code to your specific requirements, including handling errors and customizing the email content based on your Email Template structure. Additionally, ensure that your Salesforce user has the necessary permissions to execute Apex and send emails.
Now, for the second part, we will send an email notification whenever a case is resolved via Email-to-Case in Salesforce using Flow and an Email Template. You can follow a similar approach as mentioned before. However, this time, you'll need to trigger the Flow when the case is resolved. Here's a step-by-step guide:
Create an Email Template:
In Salesforce, navigate to Setup. In the Quick Find box, enter "Classic Email Templates" or "Lightning Email Templates" based on your Salesforce version. Create a new Email Template or use an existing one for the resolution notification.
Create a Flow:
Go to Setup.
In the Quick Find box, enter "Flows" and select "Flows."
Create a new Flow by clicking on "New Flow."
Use the Flow Designer to build your flow. Here's a simplified example of a flow that sends an email notification when a case is resolved:
+---------------------+
| Start |
| (Record Trigger) |
+---------------------+
|
V
+---------------------+
| Record Lookup |
| (Lookup Case) |
+---------------------+
|
V
+---------------------+
| Decision |
| (Is Case Resolved?) |
+---------------------+
| |
| Yes | No
| |
V V
+---------------------+ +-----------------------+
| Send Email |--------| Update Case (Optional)|
| (Use Email Template)| | |
+---------------------+ +-----------------------+
The Flow should start with a Record Trigger for the Case object.
Add a Record Lookup to check if the case is resolved (use the status field or any custom field that indicates resolution).
Use a Decision element to check if the case is resolved.
If the case is resolved, proceed to the Send Email element, using the Email Template you created.
Optionally, you can add an Update Case element to update the case record, such as setting a "Notification Sent" flag.
Activate the Flow:
Once you've created and tested your Flow, activate it. Configure Email-to-Case:
In Salesforce, navigate to Setup. In the Quick Find box, enter "Email-to-Case." Configure your Email-to-Case settings to use the Flow you created for the "On Case Resolution" trigger.
Test:
Resolve a test case either manually or through your Email-to-Case process. Ensure that the resolution triggers the Flow and the email notification is sent.
For the code snippet, you can use a similar Apex class as before with a method tailored for the resolution notification. Here's an example:
// Apex Class for sending an email on case resolution in Flow
public class FlowEmailOnResolution {
// Method to send email notification on case resolution
public static void sendResolutionNotification(Id caseId, Id emailTemplateId) {
// Load the case record
Case myCase = [SELECT Id, Subject, Description, ... FROM Case WHERE Id = :caseId LIMIT 1];
// Load the email template
EmailTemplate emailTemplate = [SELECT Id, Subject, HtmlValue, Body FROM EmailTemplate WHERE Id = :emailTemplateId LIMIT 1];
// Create an Email Message
Messaging.SingleEmailMessage emailMessage = new Messaging.SingleEmailMessage();
emailMessage.setSubject(emailTemplate.Subject);
emailMessage.setHtmlBody(emailTemplate.HtmlValue);
emailMessage.setPlainTextBody(emailTemplate.Body);
emailMessage.setToAddresses(new String[] { 'recipient@example.com' }); // Replace with the recipient's email address
// Attach the case information to the email
emailMessage.setWhatId(myCase.Id);
// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { emailMessage });
}
}
As before, you would use an "Apex Action" in your Flow to invoke this Apex class and pass the necessary parameters such as the caseId and emailTemplateId. Customize the code based on your specific requirements and the structure of your Email Template.