Apex email actions available for Process Builder and Lightning Flow Builder leveraging the SingleEmailMessage Class.
Features | Getting Started | Usage | FAQs | Documentation | Contributing | Acknowledgements 🥰
Invocable actions, also known as dynamic actions, can be invoked from Flows, Processes and a common endpoint in the REST API.
Salesforce administrators have the aforementioned declarative tools at their disposal, but currently only have a primitive ability to send emails. These custom Apex email actions provide the following additional capabilities:
- Available anywhere @invocable methods can be invoked. For example, Lightning Flow Builder and Process Builder.
- Save as Activity. Save a reference to the email as an activity feed item for a specified target object.
- Flexible recipients. In additional to "To" recipients, send carbon copies (CC) and/or blind carbon copies (BCC).
- Lightning Email Templates. Upgrade from Classic Email Templates to the newer Lightning Email Templates which allows global headers and footers.
- Include attachments. Attach Document, ContentVersion, or Attachment items to the email.
- Other supported options. Control other email options.
There are a few items you need to setup before installing:
- You will need to Enable Lightning Experience if you plan on using Lightning Enhanced Letterheads and Email Templates.
- You will need to Enable My Domain if you plan on using Lightning Enhanced Letterheads and Email Templates.
Deploy the actions:
- Click one of the "Deploy Buttons".
- Select the Org Type (Production / Sandbox).
- Login to your Org.
- Deploy the code.
- Assign the qsyd_Invocable_Email_Actions Permission Set to any users that will trigger email sends.
There are 2 actions included:
- QSyd - Invocable Email Action
- QSyd - Invocable Email Template Action
The latter is intentionally opinionated for exclusive use with a Lightning Email Template. The premise behind these actions is to expose the full capability of Salesforce emails, and as such the api is deliberately comprehensive.
To declaratively invoke these actions, do the following:
- Load the Process Builder or Lightning Flow editor
- Add an Action
- Select the preferred action: "QSyd - Invocable Email Action" or "QSyd - Invocable Email Template Action"
- Populate the required fields
- Populate other logically required fields, eg. At least 1 Recipient field (To, Cc, Bcc or Target Object Id)
- Optionally associate the email with a related record via the "What Id"
An Apex error occurred: qsyd_InvocableEmailBase.InvocableEmailException: >>> qsyd_InvocableEmailAction.sendEmail failed with exception: >>> Exception executing qsyd_InvocableEmailBase.send: Recipient not provided. Please provide at least one of the following: toAddress, ccAddress, bccAddress, targetObjectId
private static void given_requiredEmailParametersAreProvided_when_anEmailIsInstantiated_then_anEmailIsSent() {
qsyd_InvocableEmailAction.InvocableEmailParam param = new qsyd_InvocableEmailAction.InvocableEmailParam();
List<qsyd_InvocableEmailAction.InvocableEmailParam> params = new List<qsyd_InvocableEmailAction.InvocableEmailParam>();
initialiseSetupTestData();
Test.startTest();
param.toAddress = '[email protected]';
param.ccAddress = '[email protected]';
param.bccAddress = '[email protected]';
param.throwExceptionForSendErrors = true;
param.subject = 'Email Subject';
param.bodyPlainText = 'Plain text body';
param.bodyHtml = '<html><body><strong>Rich text body</strong></body></html>';
param.charSet = 'utf-8';
param.attachmentIds = CONTENTVERSION_EXAMPLES;
param.whatId = CASE_EXAMPLE;
param.parentMessageIds = INREPLYTO_EXAMPLE;
param.orgWideEmailAddress = '';
param.emailOptOutPolicy = 'FILTER';
param.saveAsActivity = true;
params.add(param);
List<qsyd_InvocableEmailResult> results = qsyd_InvocableEmailAction.sendEmail(params);
Integer invocations = Limits.getEmailInvocations();
// Email was sent
System.assertEquals(1, invocations);
Test.stopTest();
}
* Refer to the test class for complete working examples.
@IsTest
private static void given_requiredEmailParametersAreProvided_when_anEmailIsInstantiated_then_anEmailIsSent() {
qsyd_InvocableEmailTemplateAction.InvocableEmailParam param = new qsyd_InvocableEmailTemplateAction.InvocableEmailParam();
List<qsyd_InvocableEmailTemplateAction.InvocableEmailParam> params = new List<qsyd_InvocableEmailTemplateAction.InvocableEmailParam>();
initialiseSetupTestData();
Test.startTest();
param.toAddress = '[email protected]';
param.emailTemplate = 'Test Template';
param.targetObjectId = CONTACT_EXAMPLE;
params.add(param);
List<qsyd_InvocableEmailResult> results = qsyd_InvocableEmailTemplateAction.sendEmail(params);
Integer invocations = Limits.getEmailInvocations();
// Assert an email was sent
System.assertEquals(1, invocations);
Test.stopTest();
}
* Refer to the test class for complete working examples.
Option | Description |
---|---|
Recipient To Addresses |
Recipient To Addresses, Max 100, Comma Delimited |
Recipient Cc Addresses |
Recipient Cc Addresses, Max 25, Comma Delimited |
Recipient Bcc Addresses |
Recipient Bcc Addresses, Max 25, Comma Delimited |
Email Template Id or Name |
Id, Name, or Developer Name of Email Template |
Email Subject |
Email Subject |
Email Plain Text Body |
Email Plain Text Body |
Email Html Body |
Email Html Body |
Email Character Set |
The character set for the email. If this value is null, the user's default value is used |
Attachment Ids |
Comma delimited list of Document, ContentVersion, or Attachment Ids |
Target Object Id - Contact, Lead or User Id |
The Id of the contact, lead, or user to whom the email will be sent |
What Id |
If you specify a contact for the targetObjectId field, you can specify an optional whatId as well. Must be either a Account, Asset, Campaign, Case, Contract, Opportunity, Order, Product, Solution, Custom |
Parent Message Id |
This field identifies the email or emails to which this email is a reply (parent emails) |
Email Opt Out Policy |
If you added recipients by ID instead of email address and the Email Opt Out option is set, this method determines the behavior of the sendEmail() call |
Org Wide Email Address |
The associated org wide email address set up in Organization-Wide Addresses |
Save as Activity? |
This argument only applies if the recipient list is based on targetObjectId or targetObjectIds. If HTML email tracking is enabled for the organization, you will be able to track open rate |
Use Signature? |
Indicates whether the email includes an email signature if the user has one configured |
Treat Bodies as Template? |
The subject, plain text, and HTML text bodies of the email are treated as template data. The merge fields are resolved using the renderEmailTemplate method |
Treat Target Object as Recipient? |
If set to true, the targetObjectId (a contact, lead, or user) is the recipient of the email. If set to false, the targetObjectId is supplied as the WhoId field for template rendering but isn’t a recipient of the email |
Throw an Exception for Send Errors? |
Throw an exception containing any send results errors. The default is true |
Read the wiki for documentation on Invocable Email Actions.
Read the FAQs answer common questions.
Yes
Yes
Yes
Yes, labels can be translated using Salesforce Translation Workbench
See the list of Contributors who participated in this project.
If you would like to join these awesome people, please refer to contributing.md for guidelines.
Special thanks to:
- Q Branch Sydney for your continued support.
- Everyone that has requested for, used and provided feedback on this project.
Copyright © 2020 Q Branch