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.
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
- 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.
- You may need to click the Site label to go back into the Site again.
- 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 email@example.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.
- 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.
- Open Custom Metadata Types from Setup.
- Click Manage Records.
- Click New.
- Label and name the record “Error email list” or anything else you want. It doesn’t matter to this process.
- In Comma Separated Email List, type the list of anyone who should be alerted when a Flow fault occurs. Separate the emails with commas.
- 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
- 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.
- The next page should look like this.
- 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!
- Unsubscribe Part 1
- Unsubscribe Contacts
- Process Builder
- Custom Object
- Custom Fields
- Custom Fields
- Visualforce Page
- Email Template Folder “Unsubscribe”
- Unsubscribe Contact HTML Template
- Unsubscribe Link VF Contacts
- Unsubscribe Link VF Leads
How It Works Technically
- Email recipient clicks unsubscribe link in email.
- This launches screen Flow Unsubscribe Part 1
- Which is embedded in Visualforce Page Unsubscribe
- Which is the main page of a Site (not included in package)
- Which is which is set to be visible to external users.
- 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).
- If the flow runs successfully, it sends a confirmation email to the person who unsubscribed.
- 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.
- The record creation launches Process Builder UnsubscribePB.
- 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.
- 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.
- 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.
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
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.