Skip to content

paull10au/qsyd_InvocableEmailActions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stars Forks Downloads Issues

Invocable Email Actions

The Full Power of Salesforce Emails and Lightning Templates - Declaratively!

Apex email actions available for Process Builder and Lightning Flow Builder leveraging the SingleEmailMessage Class.


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:

  1. You will need to Enable Lightning Experience if you plan on using Lightning Enhanced Letterheads and Email Templates.
  2. You will need to Enable My Domain if you plan on using Lightning Enhanced Letterheads and Email Templates.

Deploy the actions:

Deploy to Salesforce Deploy using SFDX
  1. Click one of the "Deploy Buttons".
  2. Select the Org Type (Production / Sandbox).
  3. Login to your Org.
  4. Deploy the code.
  5. 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:

  1. Load the Process Builder or Lightning Flow editor
  2. Add an Action
  3. Select the preferred action: "QSyd - Invocable Email Action" or "QSyd - Invocable Email Template Action"
  4. Populate the required fields
  5. Populate other logically required fields, eg. At least 1 Recipient field (To, Cc, Bcc or Target Object Id)
  6. Optionally associate the email with a related record via the "What Id"

⚠️ Exceptions due to incorrect configuration or technical send errors will be sent as the standard email or can be displayed as a handled fault within the Lightning Flow Builder.

Example exception

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

A simple use case for Process Builder:

A simple use case for Lightning Flow:

A programmatic example:

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.

Example lightning email template:

A simple use case for Process Builder:

A simple use case for Lightning Flow:

A programmatic example:

   @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.

Supported options

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.

Does it work in Communities?

Yes

Does it work in Mobile?

Yes

Does it work with Person Accounts?

Yes

Does it support Internationalisation (i18n)?

Yes, labels can be translated using Salesforce Translation Workbench

Others?

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.

Acknowledgements

Special thanks to:

  • Q Branch Sydney for your continued support.
  • Everyone that has requested for, used and provided feedback on this project.

License

License Copyright © 2020 Q Branch

About

Invocable Email Actions

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages