How to Include an Unsubscribe Link, Revised

4/27/20: I am unable to provide support for this app over the next few months. My apologies for the inconvenience! 

2/15/20: Instructions and set up worksheet updated for Spring ’20.

1/9/20: Download the set-up worksheet to use with the instructions.

12/16/19 Download the app now on the AppExchange!

Allow Recipients to Unsubscribe from All Emails

Follow this tutorial to include an unsubscribe link in promotional emails sent from Salesforce. This post gives you some reasons for considering this feature.

When the recipient clicks to unsubscribe, a flow will look for all contacts and leads who have this as their preferred email address (if you’re in NPSP) or in the Email field. All contacts or leads who meet that requirement will be marked “Email Opt Out.”  The email address owner will receive one confirmation email immediately.

Please try this in your sandbox, developer org or Trailhead playground first! 

The difficulty level for this tutorial is medium because of all the steps. You should know how to modify a page layout and a profile, create or modify an email template, and use a sandbox. You will install a package that contains Flow and Process Builder, but you don’t need to know how to use them.

New in Spring ’20: Run Flows in System Mode

Learn more about this new feature.

  • Open Unsubscribe Part 1 Flow. 
  • Click Save As.
  • Click Show Advanced.
  • Under “How to Run the Flow,” select System Context with Sharing–Enforces Record-Level Access. Click Save. Click Activate.

Create a site 

  • (Setup -> User Interface -> Sites and Domains -> Sites)
  • Create a new Site.
  • If you don’t have a Site domain, you’ll need to set one up. You may want to use the same domain as your “My” domain. Keep in mind: if I make my site domain “BaileyBrothers,” then the unsubscribe confirmation email will appear from “BaileyBrothers Site Guest User.” I will look into how to modify that label.
  • Site Name & Label: Unsubscribe
  • Site Contact: lookup your own user record here.
  • Active Site Home Page: find the visualforce page Unsubscribe.
  • A default web address does not need to be specified.
  • Save and Activate.
  • Copy and save the Site URL for use in a later step.

site url

  • You may need to click the Site label to go back into the Site again.

Sharing Settings

  • Enable the External Sharing model.
  • Internal Lead sharing must be set to Public, which will allow external lead sharing to be public.
  • Default External Access for Accounts needs to be Public (Read only or Read/Write) and Contacts need to be controlled by parent or Public (Read only or Read/Write).

Sharing Rules in Spring ’20

New in Spring ’20 security you have the option to lock down security for Site Guest Users. On your Sharing Settings page, look for a checkbox “Secure guest user record access.” If that’s checked, you’ll need to add a sharing rule for Leads and for Accounts. You can’t create sharing rules for Contacts, but you need to create one for their parent object Accounts. Here’s a video.

Update variables in the flows

  • The variables in Flow called Unsubscribe Part 1.
    • OrganizationName – otherwise defaults to “our organization”
    • SenderAddress – otherwise defaults to fake@fake.com. This is the email address that the unsubscribe confirmation email comes from.
    • Plain text email body – You’ll find this under Formulas. Change the default text of the confirmation email from “You will no longer receive email from (Organization Name).”
    • Follow the gif below to update “our organization” to the name of your actual organization. Look for SenderAddress in the Manager tab as well.

2019-09-09_15-17-11

  • Click “Save As” and Close.
  • Activate this latest version of the Flow.

Required: One Time Set Up of Error Recipient Email Address(es)

This package contains a Custom Metadata Type to store the email addresses of recipients of the error handling message. You’ll need to create a record.

  1. Open Custom Metadata Types from Setup.
  2. Click Manage Records. Manage records to the left of Error Email Recipients
  3. Click New.
  4. Label and name the record “Error email list” or anything else you want. It doesn’t matter to this process.
  5. In Comma Separated Email List, type the list of anyone who should be alerted when a Flow fault occurs. Separate the emails with commas.Image of a custom metadata type record called Error Email Recipients. The record has one field Comma separated Email List that has a list of three emails.
  6. Click Save.

Set Up Email Templates

The package contains a folder of classic email templates called Unsubscribe.

Classic Email Templates

    • Unsubscribe Contact HTML Template
    • Unsubscribe Link VF Contacts Template
    • Unsubscribe Link VF Leads Template

Open the email templates and find the instructions for modifying the unsubscribe link to work for your site. The HTML template does not work for email addresses that include special characters like plus signs, so I’d recommend using the Visualforce (VF) email templates if you can.

Lightning Email Templates

Lightning email templates are not able to be included in packages at this time, so you’ll need to create your own.

  • Search for Email Templates or Lightning Email Templates in your AppLauncher.
  • Name it whatever you like.
  • In Related Entity Type choose Contact.
  • Click on the button Source and paste in this HTML
Click here to unsubscribe from all emails from our organization./a>
  • Modify the HTML of the link above by changing this part:
    sdo-demo-main-164dcce81f8.secure.force.com to the Site URL you saved earlier.
  • Change “our organization” to the name of your organization.
  • Write the rest of the email, including a subject and save.

Test it out

  • In your sandbox, make sure Email Deliverability is turned on.
    • Go to setup. Search for “Deliverability.”
    • Set the access level to “All email.”
    • Be very careful if you are using a sandbox with real data – do you have any automated processes that are now going to start firing off emails like crazy? Remember to turn off email deliverability when you’re done testing.
  • Optional: Edit your Contact page layout.
    • Add the Email Opt Out field.
    • Add the Unsubscribe related list. You may want to add other fields here like Unsubscribe Email, Created Date, Status
  • Create a contact with your email address.
  • TEST IT!
    • Send an email to that contact (using the template you created) to test it. Make sure the contact has “Email Opt Out” unchecked.
    • When you click the link it should look like this.

applefence

  • The next page should look like this.

applefence2

  • Look for that confirmation email.
  • Open the contact record and confirm the following:
    • Email opt out is now checked
  • There is a new related Unsubscribe record and Status = “Unsubscribed.”

YOU DID IT!

Package Contains

  • Flows
    • Unsubscribe Part 1
    • Unsubscribe Contacts
    • UnsubscribeLeads
  • Process Builder
    • UnsubscribePB
  • Custom Object
    • Unsubscribe
      • Custom Fields
        • Contact__c
        • Lead__c
        • Unsubscribe_Email__c
        • Status__c
  • Visualforce Page
    • Unsubscribe
  • Email Template Folder “Unsubscribe”
      • Unsubscribe Contact HTML Template
      • Unsubscribe Link VF Contacts
      • Unsubscribe Link VF Leads

How It Works Technically

  1. Email recipient clicks unsubscribe link in email.
  2. This launches screen Flow Unsubscribe Part 1
    1. Which is embedded in Visualforce Page Unsubscribe
    2. Which is the main page of a Site (not included in package)
    3. Which is which is set to be visible to external users.
  1. If the Flow errors out, it looks up a Custom Metadata Type record Error Email Recipient and sends an email to the email addresses listed there (most likely your admin).
    1. If the flow runs successfully, it sends a confirmation email to the person who unsubscribed.
  1. The flow creates a new Unsubscribe record, a child record of the Contact. The external user has permission to create this kind of custom record but cannot create or modify standard objects.
  2. The record creation launches Process Builder UnsubscribePB.
    1. Why Process Builder instead of staying in the same Flow? Because by breaking it up to have a Process run in the middle, the External User is not technically modifying the contact or lead objects. External Users cannot modify standard objects. The Process Builder is checking the email opt out box on contact and lead.
  3. The process launches the Unsubscribe Contacts flow which finds all contact records with this email address as their preferred email and marks them Email Opt Out.
  4. The process also launches the Unsubscribe Leads If the whole process works successfully, the Unsubscribe record is marked as Status = “Unsubscribed”. If there is an error, Status = “Error” and an alert will be sent to people set as error recipients.

Revision notes

April 27, 2020

You no longer need to give permission for the Site Guest User to access certain objects or fields. Instead run the flow in System Mode. Instructions above. 

December 18, 2019

Here are instructions on how to remove the confirmation email from the Flow.

v.1.11 December 16, 2019

Now on AppExchange!

V.1.11 November 26, 2019

I added a missing step to the instructions and fixed some errors in the email templates.

V. 1.10 October 9, 2019

I have revised this one to submit for security review with the AppExchange. With security improvements to Sites in Winter ’20, I’ll need to add additional improvements very soon.

Now includes Leads as well as Contacts. Includes Custom Metadata instead of hardcoding emails in the Flow.

More resources

17 Comments

  1. Thanks so much for this, I have tried testing it but I am receiving this error: Error element Create_Unsubscribe_Record (FlowRecordCreate).
    This error occurred when the flow tried to create records: INVALID_FIELD_FOR_INSERT_UPDATE: Unsubscribe__c: bad field names on insert/update call: Contact__c, Status__c, Unsubscribe_Email__c, Lead__c. Do you know what could be the issue here? Thanks again.

    Reply

    1. Have you given permission to your site guest user to access all of those fields on the Unsubscribe object?

      Reply

  2. Not sure what I’m missing here, but I can’t find this:

    (Setup -> User Interface -> Sites and Domains -> Sites)

    Reply

    1. Edit – nevermind, sites are unavailable in professional edition. Therefore, this app is unavailable in professional edition

      Reply

      1. Bummer! Sorry about that! Check on AppExchange for edition requirements when installing.

    1. thanks for trying the app. I need more information. you could email me at jrymph @ salesforce .com

      Reply

  3. Hey,
    I have completed the process and can send the test mail and unsubscribe.

    Next, how do I automatically add the “unsubscribe” button to the existing or newly created email campaigns?

    Reply

  4. Hey awesome people,

    Our client complained about the “Your flow is finished” end page. And we attempted to modify your Visualforce Markup of VF page “Unsubscribe” to redirect into client domain (see line 6 below):

    Any ideas where did we exactly mess up on the code to enable the redirect to homepage instead of seeing “Your flow is finished”?
    Or completely get rid of the “Your flow is finished”? page.

    Reply

    1. Hello! Sorry that I didn’t respond to your earlier comment. Looks like you figured out how to update the templates? don’t see your code here. I will hopefully have time to look into your issue before I go on leave! Thanks for your patience.

      Reply

  5. Hello – and thanks for this app!
    I installed and configured, I tested and works perfectly.
    I have one question though – I can’t seem to be able to make it work with Lightning Email Templates. I’ve tried to insert the Unsubscribe link in the templates – but I get an error: “This email template has two kinds of merge fields. Remove or replace merge fields in the format {!object.field}, such as {!Contact.FirstName}. Use the merge field picker to insert merge fields with the format {{{Object.field}}}.: HTML Value”

    Any thought on this?
    All our current templates are Lightning so it would be great if we could make unsubscribe work with these.

    Reply

    1. Thanks for trying the app! First dumb question, did you find the instructions above on working in Lightning templates?

      Reply

  6. Hi Jessie,

    Instructions were pretty straight forward. i believe i configured everything properly. However, when i do a test, and click the unsubscribe link i get a page that simply says “Your Flow Finished”.

    What am i missing?

    Reply

Leave a Reply