1

I have been lately exploring on how to send bulk emails from flow. There is documented way to send single email using singleEmail action as described here. I implemented this and new I can successfully convert

Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
/*some awesome code*/   
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail});

to

Map<String, Object> flowVariables = new Map<String, Object>{
                'body' => htmlBody,
                'toAddresses' => toAddresses,
\*some more keys as required by sendEmail action*\                    
            };

Flow.Interview.SendEmail sendEmail = new Flow.Interview.SendEmail(params);
sendEmail.start();

enter image description here

However I wanted to send bulk emails i.e., I wanted to convert this

Messaging.SingleEmailMessage[] emails = new List<Messaging.SingleEmailMessage>();
for(/*some iteration*/){
    Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
    /*some awesome code*/
    emails.add(semail );
}
 Messaging.sendEmail(emails);

to

List<Map<String, Object>> bulkParams= new List<Map<String, Object>>;
for(/*some iteration*/){
    

    Map<String, Object> flowVariables = new Map<String, Object>{
                    'body' => htmlBody,
                    'toAddresses' => toAddresses,
        \*some more keys as required by sendEmail action*\
        }
    bulkParams.add(flowVariables );
};

The approach I was using was to directly invoke the flow inside loop which I am very doubtful if its a good practice as shown

for(/*some iteration*/){    
    Map<String, Object> flowVariables = new Map<String, Object>{
                    'body' => htmlBody,
                    'toAddresses' => toAddresses,
        \*some more keys as required by sendEmail action*\
        }
    Flow.Interview.SendEmail sendEmail = new Flow.Interview.SendEmail(bulkParams);
    sendEmail.start();
};

Also adding to that I had few more doubts, the document mentions followinf for email recipients

For the email to send successfully, enter a value for Email Addresses (comma-separated) or Email Addresses (collection). You can use both parameters as long as the combined number of email addresses is five or fewer. This parameter accepts single-value resources of any type. That value is treated as text.

So if we provide email addresses of internal users, it would count in SingleEmail against the limit?

2
  • 1
    same body is sent to n emails? or is calling context going to send body0 to emails (a,b,c); body1 to emails (d,e,f)?
    – cropredy
    Commented Mar 7, 2023 at 17:00
  • @cropredy the body can vary between email addresses
    – AmanSharma
    Commented Mar 8, 2023 at 14:53

1 Answer 1

1

Several things going on here

Given that you asserted in the comments that within a transaction that body0 could apply to emails [email protected], b.example.com but body1 could apply to emails [email protected], [email protected], e.example.com

Then:

You will need to call the Flow.Interview start() method for each unique bodyX within the transaction

To address the 5 emails per Flow Send Email action limit you have two choices

  • Change your calling program to ensure that at most 5 emails are sent per bodyX, potentially calling the Flow.Interview start() method more than once for a single bodyX (e.g. if you have 6 emails for bodyX, then first call to start() provides 5 emails and second call to start() provides the 6th email
  • Don't use the Send Email action but instead use the UnofficialSF.com Send Better Email Flow Action. This doesn't have these limits as it uses Apex to send the emails

As for limits, consult the Email Limits guide

Each licensed org can send single emails to a maximum of 5,000 external email addresses, or recipients, per day. A day is based on Greenwich Mean Time (GMT).

Sending emails to internal email recipients doesn't count toward the org daily limit.

For orgs created before Spring ’19, the org daily limit is enforced only for emails sent via Apex and Salesforce APIs, except for REST API.

For orgs created in Spring ’19 and later, the org daily limit is also enforced for email alerts, simple email actions, Send Email actions in flows, and REST API.

2
  • Okay , So you basically mean Apex is Better than Flow? Since it offers more flexibility. Is there any good reason to use flow? @cropredy
    – AmanSharma
    Commented Mar 9, 2023 at 5:10
  • I didn't say that; you can use Flow; just that the if your data has > 5 emails per Flow Interview, adjust your caller to make sure that each Interview has no more than 5 emails. I presumed the Flow has other functions besides sending emails (either now or in the future).
    – cropredy
    Commented Mar 9, 2023 at 5:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .