Salesforce Development: How to create custom welcome and password reset emails for Communities Part 1
Why Would We Need to Create Custom Welcome and Password Reset Emails?
There are a handful of reasons that you would need to create custom password reset and welcome emails for your Salesforce Communities. The most common reasons are the following:
1) The out of the box (OOTB) custom password reset and welcome emails are just bland and boring. They are regular text templates with not a lot to them.
2) There are multiple users types in your community. Some users need to see different things in their welcome and password reset emails, so we need to render different sections of the emails for different user types.
3) Some users sign in via Single Sign On (SSO) and don’t need a password reset email, but some users aren’t allowed to use SSO and do need to sign on via the normal Salesforce community login page.
All of these are legitimate and relatively common business cases for customizing your password reset and welcome emails, that being said Salesforce doesn’t make this process easy or document it well, so let’s dive in to how we can achieve these custom emails.
Setting Up Our Custom Visualforce Email Template
The first thing we need to do, after activating our community and setting membership for the community (these things are covered in the video above), is setup a visualforce email template to create our custom pass reset and welcome emails.
To create a new vf email template, do the following:
1) Go to Setup -> Classic Email Templates and click the “New Template” button.
2) Select the “Visualforce” template type
3) Fill out the “Email Template Information” section with whatever you’d like.
4) In the “Visualforce Template Attributes section, make the “Recipient Type” Contact and the “Related To Type” Network
5) Click the “Save” button
There is a ton of conflicting information on the internet about what to make the Recipient Type and Related To Type for these email templates. CONTACT AND NETWORK SHOULD ALWAYS BE WHAT YOU USE IN THIS INSTANCE! You might be like, “But why bro? Why that matter at all?”. For these password reset and welcome emails for community members these objects seems to work best without any problems (I’ve tested tons of combinations). You get merge field issues when you utilize the User object instead of the Contact object for recipient and you get the most additional useful information from the Network object (which is the object that stores your community information in SF). Feel free to do more investigation yourself but I have wasted days of my life on this, lol… it wasn’t super fun.
The Visualforce Email Template Code and The Magical Global Network Merge Field
After you’ve created/saved your visualforce email template, press the “Edit Template” button. You should see something like the code below:
<messaging:emailTemplate subject="wow" recipientType="Contact" relatedToType="Network">
<messaging:plainTextEmailBody >
Congratulations!
This is your new Visualforce Email Template.
</messaging:plainTextEmailBody>
</messaging:emailTemplate>
You should go on ahead and change it to something like this (the words inside the paragraph tag can be anything):
<messaging:emailTemplate subject="wow" recipientType="Contact" relatedToType="Network">
<messaging:htmlEmailBody >
<p>Hi there welcome to the community</p>
{!$Network.NetworkUrlForUserEmails}
</messaging:htmlEmailBody>
</messaging:emailTemplate>
So… what did I change? There are two things:
1) I changed the <messgaing:plainTextEmailBody> tags to <messaging:htmlEmailBody> tags. This allows us to make an email using html instead of just plain text.
2) I added the paragraph tag and the most magical merge field in the system (at least for this blog post) !$Network.NetworkURLForUserEmails
Let me tell you… Several years ago, when I first attempted this, I googled a ton of different ways before I finally found this merge field. This network merge field normally gives you the url for the login page of the community, but if this is in a welcome email or a reset password email for the community it will create a link for the reset password page! This is the key to making our custom password reset emails… but there’s even more to consider with this, unfortunately.
How to Test Your New Custom Welcome and/or Password Reset Email
You might be thinking, “Yes! This Network merge field solves all my problems”, but there’s one more thing you need to take into consideration (at least) and that’s how to test this new custom email you’ve made. If you try to use the, “Send Test and Verify Merge Fields” button to test this email, you’ll notice that it doesn’t work… the password reset link just won’t send… great. So what do you do?
To test this email we need to do the following:
1) Go back to your community and override the password reset emails in the Workspaces Emails tab.
2) Create a contact that has (at least) a first name, last name, email and account associated to them.
3) Enable the contact as a community user
4) Make sure the community user for the contact has a profile or permission set that makes them a member of the community. Also make sure they have an email associated to their user that you have access to.
5) Grab the Id for the user you just created from the URL of your user record
6) Open the developer console
7) Open the Execute Anonymous Window in the Developer Console
8) Paste the code below into the Execute Anonymous Window and then click the “Execute” button
System.resetPassword('[Your user Id you copied above]', true);
After running that System.resetPassword(userId, sendEmail) method in the execute anonymous window you should receive an email with a password reset link inside it! Wooooooooooooooooo!!! We did it!! We got an email setup that we can heavily customize that we can also get a password reset email in! Depending on your needs though… this may not be enough. There are a few more caveats to get through if you need different things rendered for different community member types, etc.
Getting Technical With It
If you need to segment out your emails and/or render things conditionally for different users, move on to the next blog post here.
We go over how to build Visualforce Components to conditionally render elements of your email based on the user we’re sending the email to, the extremely interesting problem doing that with Welcome emails and how to setup a trigger on the User object to determine who to send different emails to.
Get Coding With The Force Merch!!
We now have a redbubble store setup so you can buy cool Coding With The Force merchandise! Please check it out! Every purchase goes to supporting the blog and YouTube channel.
Get Shirts Here!
Get Cups, Artwork, Coffee Cups, Bags, Masks and more here!
Check Out More Coding With The Force Stuff!
If you liked this post make sure to follow us on all our social media outlets to stay as up to date as possible with everything!
Youtube
Patreon
Github
Facebook
Twitter
Instagram
Salesforce Development Books I Recommend
Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps