How to Add CC Recipients Dynamically?

Spread the love

Question

In Salesforce Flow, adding CC recipients dynamically can be achieved using email alerts and flow configurations. Here’s how you can implement this:

How Can We Dynamically Add Recipients to an Email Alert in Salesforce?

Is it possible to dynamically add recipients to an email alert in Salesforce, possibly using a custom field or another method? On button click, it should prompt for email addresses, and those addresses should be added to the email alert’s recipient list based on specific conditions.

Solution 1: Using Email Alerts

First, create an email alert on the relevant object, such as the Account object. While setting up the email alert, you can specify the primary recipients (e.g., Account Owner) and add CC recipients in the “Additional Emails” section.

When you use this email alert in a flow, you can pass the record ID if the flow operates on the same object as the email alert. The CC recipients defined in the email alert will automatically be included when the email is sent.

Example Email Alert:

  1. Create an email alert for the Account object.
  2. Specify the recipients, such as the Account Owner.
  3. Add CC addresses in the “Additional Emails” field.
  4. Specify the “From Email Address” as desired.

Example Flow Configuration:

  1. In the flow, add the “Send Email Alert” action.
  2. Select the email alert created earlier.
  3. Pass the relevant record ID to the action to trigger the email alert.

Solution 2: Using Apex for More Flexibility

If you need dynamic control over the CC addresses based on specific criteria or inputs at runtime, you can use an Apex class to send the email. You can then invoke this Apex class from the flow.

Here is an example of an Apex class that sends an email with dynamic CC recipients:

public with sharing class EmailHelper {
    @InvocableMethod
    public static void sendEmail(List<EmailRequest> requests) {
        for (EmailRequest req : requests) {
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setToAddresses(req.toAddresses);
            email.setCcAddresses(req.ccAddresses);
            email.setSubject(req.subject);
            email.setPlainTextBody(req.body);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
        }
    }

    public class EmailRequest {
        @InvocableVariable
        public List<String> toAddresses;
        @InvocableVariable
        public List<String> ccAddresses;
        @InvocableVariable
        public String subject;
        @InvocableVariable
        public String body;
    }
}

Code Explanation:
The EmailHelper class contains an invocable method sendEmail, allowing flow integration. It iterates over input EmailRequest objects to configure and send emails dynamically. The EmailRequest inner class defines parameters like toAddresses, ccAddresses, subject, and body, exposed to flows via the @InvocableVariable annotation. This provides flexibility in passing runtime values, such as dynamic recipient lists, directly from the flow.

To use this class in a flow:

  1. Add an “Apex Action” element in your flow.
  2. Select the EmailHelper.sendEmail Apex method.
  3. Provide the necessary inputs such as toAddresses, ccAddresses, subject, and body.

Both solutions are effective depending on your use case. Use email alerts for simple configurations and Apex for more dynamic and complex requirements.

Summing Up

The answer explains how to dynamically add CC recipients in Salesforce using Email Alerts and Salesforce Flows. It provides two methods: setting up CC recipients through “Additional Emails” in an Email Alert or by creating a custom solution using Apex. The Apex solution involves constructing an email using the Messaging.SingleEmailMessage class, setting dynamic CC addresses, and sending the email. Each code snippet is explained, highlighting its purpose and usage within the context of Salesforce.

Job-Oriented Salesforce Course: Enroll for Free Demo

Our Salesforce Course is designed to deliver a thorough understanding of the Salesforce platform, equipping you with the essential skills to excel in the CRM domain. The curriculum includes key modules like Salesforce Admin, Developer, and AI, seamlessly blending theoretical concepts with practical application. With real-world project experience and hands-on exercises, you’ll develop the expertise to tackle complex business challenges using Salesforce solutions. Our seasoned instructors ensure you gain valuable technical knowledge and industry-relevant insights to thrive in the Salesforce ecosystem.

Beyond technical expertise, our Salesforce training in Chennai provides personalized mentoring, certification support, and interview coaching to enhance your career prospects. Participants benefit from comprehensive study materials, practical exposure through live projects, and individualized guidance throughout the course. By the program’s conclusion, you’ll be well-prepared not only for certification exams but also for real-world scenarios, armed with the problem-solving skills and hands-on experience employers value. Take the first step in your Salesforce career with us and open the door to a world of exciting opportunities!


0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Open Chat
1
Dear Sir/Madam
How can I help you?