Why On Earth Would We Ever Build Trigger Bypasses?
This is an excellent question and one I wondered myself… until my team and I had to do a monstrous 50,000,000 record load into Salesforce for a deployment, lol. I was stumped… how on earth could I ever pull this off without taking days or weeks to upload that data. We’re not gonna into all the detail behind that monster data load, but we’ll just say turning off the triggers was a part of it. Then we were faced with the, you can’t turn off the system while we do this data load and I was like, fml, how though? As it turns out there is a way! And a really good and simple way if you leverage Custom Settings.
There are a number of other reasons to do this too. A data team that loads data that’s pre-transformed (doesn’t need to go through the trigger) or an integration that auto-transform the data and doesn’t need to be bogged down by a trigger. There are plenty more beyond these as well, but these are the major ones.
What are Custom Settings?
Custom Settings are much like Custom Metadata in that they are typically used in the same way as a config file in another programming language. That being said they are a bit different. Unlike custom metadata, custom settings allow you to change the values of the data you store based on the user that is currently running the code. So each user in your system can have different values for custom metadata records. Pretty nifty, if I do say so myself.
So How Do We Set This Bypass Up?
This is so simple it’s mind boggling, lol. All you need to do is setup a hierarchy custom setting object by going to Setup -> Custom Settings and then create Checkbox (Boolean) fields for each object you have a trigger for that you may want to bypass.
After you’re done setting that bad boi up. Click the “Manage” button at the top of your custom setting so that you can create records for it. After you do that you’re going to be presented with two different “New” buttons on the screen and it’s not super obvious what either of them do, so let me explain. The top “New” button is to make default values for ALL USERS in your org. This is the Default Organization Level Value. The bottom “New” button allows you to make new records that are only relevant to certain Users or Profiles. The bottom “New” button is what we want to click.
After clicking the bottom “New” button you’ll be presented with option to select a, “Location” which is the most confusing label of all time, lol. This just expects you to choose a User or a Profile which will then allow you to have unique values for Users and Profiles for your custom setting! Pretty Kewllllllll. Select the profile or user you would like to bypass your trigger(s) and select the checkboxes for the triggers they should bypass and then hit “Save”.
That’s it, pretty damn simple. Now on to the equally simple trigger code.
The Trigger Code
You might be thinking, oh no code… I’m sorry if you feel that way because code is the most amazing thing since General Tso’s Chicken. However, if you do, no worries, we need four lines of code… yes that’s it. Now before I show you this code, please note, you should be utilizing a trigger framework for your code and that no logic should live in your triggers… but that’s for another day. This is just a simple example, so lets check it out.
trigger Account_Trigger on Account (before insert)
{
//Getting the Process_Switches__c custom setting value that is relevant to our running
//user
Process_Switches__c processSwitches
Process_Switches__c.getInstance(UserInfo.getProfileId());
//If the user can bypass the trigger, return and do not continue the trigger.
if(processSwitches.Account_Process_Bypass__c)
{
return;
}
//Call your trigger handler and run trigger logic
}
Woop, there it is. Pretty simple stuff. If you’re not familiar with Custom Settings and the unique ways to query data from them the whole [Custom Setting Name].getInstance(UserInfo.getProfileId()) might look a little confusing. It’s pretty simple though. It basically gets the custom setting record that is relevant for the running users profile. This makes sure we always get the correct record and only the right people are bypassing the trigger! Pretty kewl huh? And that’s it, yep, that’s really it. Now go enjoy a trigger free world where your data loads go lightning fast.
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.
How to Render Elements of a Custom Community Password Reset and/or Welcome Email Conditionally
Alllllrighhtttttttt, so if you didn’t read the first blog post, you might get a bit lost, so be sure to check that out here.
If you have already read it, let’s figure out first why the hell you would ever want to do this conditional rendering magic. It’s because of the following:
1) You can only associate one password reset email and/or welcome email to your community (This is especially important for password reset emails as we’ll see in a bit).
2) If you need different user groups to receive different things in their welcome or password reset emails, you need to be able to conditionally render elements based on who the email is being sent to.
So now that we know why we would do this, let’s figure out how we would go about doing this.
<!--This is the visualforce component-->
<apex:component id="ToightComPassReset" access="global" controller="ToightComPassResetController">
<apex:attribute name="contactIdPassed" description="Contact id passed from email
template" type="Id" assignTo="{!contactIdPassedCtrl}"/>
<p>Hi bro wut upppppppppppppp</p>
<div style="{!IF(ProfileName='Customer Community Plus Login User', 'display:block',
'display:none')}">
<a href="{!$Network.NetworkUrlForUserEmails}">Click here to reset your
password</a>
</div>
</apex:component>
//This is the visualforce component controller
public with sharing class ToightComPassResetController
{
public Id contactIdPassedCtrl{get; set;}
public String getProfileName()
{
String profileName = '';
List<User> usersFromContact = [SELECT Profile.Name FROM User WHERE ContactId =
:contactIdPassedCtrl LIMIT 1];
if(!usersFromContact.isEmpty())
{
profileName = usersFromContact[0].Profile.Name;
}
return profileName;
}
}
So, obviously to a seasoned developer the code above is not super complicated but if you’re not familiar with it or why I wrote the component the way I did, let me explain it a bit. Here are some key pieces of the vf component:
1) Your component must be declared as access= “global” or else it will not be visible to your email template.
2) The component is declared with an <apex:attribute> tag, this tag allows you to send data to your visualforce component from the email template and pass that data to your apex controller. In our case we will be sending the contact Id from the email template and assigning it to the contactIdPassedCtrl variable in the apex controller.
3) We conditionally render a div on the page using an IF condition instead of just using a pageblock. This is because you can’t utilize pageblocks in email templates, you have to use normal html elements.
4) This conditional rendering might seem confusing, “{!IF(ProfileName=’Customer Community Plus Login User’, ‘display:block’,’display:none’)}” but it’s actually fairly simple when explained, so let me do that. Basically what it’s saying is, if the getProfileName method in our apex controller returns a value of “Customer Community Plus Login User” display our div, otherwise don’t display it.
5) If you didn’t know, in Visualforce, if your apex controllers method starts with the word get, you can reference it without the word get in the vf page/component. More examples here.
Referencing our Visualforce Component in the Email Template
Alright so now that we have our vf component and our apex controller built, let’s check out how to reference that component in our email template!
Pretty simple right? Not a lot to it. To reference the vf component we just use the component tag “<c:ToightComPassReset>” which is just <c: followed by the name of the vf component you made and we use the “contactIdPassed” parameter to pass our contacts id to the component. If you don’t remember our recipient type is equal to contact so the “recipient.Id” merge variable references the contact id. Additionally the contactIdPassed is the name of the <apex:attribute> we declared in our vf component.
Now for the frustrating truth… Welcome emails do not work with conditional rendering
You might be thinking… then why did you just waste my time. Please don’t worry, not all hope is lost. Yes, unfortunately when you use the OOTB welcome email setup for communities they for some reason do not respect the conditional rendering (at least when looking at profiles or permission sets)… it is infuriating, I tried a million different ways and I almost gave up all hope, but where there’s a will, there’s a hacky Salesforce solution, so let me walk you through it.
Thankfully, reset password emails and emails sent in the after portion of a user trigger respect conditional rendering just fine! So let’s figure out how to leverage that knowledge. Before setting up the trigger though let’s do a little more prep work.
Next build a custom vf template welcome email if you want one that differs from the password reset email.
Make sure your password reset email is still selected for the Forgot Password and Change Password email options in your community’s admin email tab.
And now… let’s make a User trigger.
Creating a User Trigger to send out community password reset and welcome emails
Yep, this is it, we’re finally on the last step. To finish this and make custom password reset and welcome emails work for your communities.
Below is some example code showing you how to do this. Fair disclaimer here, this is not how you should setup a trigger ever! You should be using a trigger framework like this one and leveraging it, but I wanted to keep this simple for those of you out there who are less familiar with triggers. Please DO NOT PUT THIS CODE IN YOUR ORG WITHOUT REWRITING IT TO BE MORE SPECIFIC FOR WHEN TO SEND OUT EMAILS!! You can also view the code on github here.
//This is our User trigger
trigger UserTrigger on User (after insert)
{
if(trigger.isAfter && trigger.isInsert)
{
CommunityEmailManager.processEmails(trigger.new);
}
}
//This is our helper class to send out emails
public with sharing class CommunityEmailManager
{
private static final String PROFILE_NAME = 'Customer Community Plus Login User';
public static void processEmails(List<User> userList)
{
Set<Id> profIds = new Set<Id>();
for(User usr: userList)
{
profIds.add(usr.ProfileId);
}
Map<Id, Profile> profilesFoundMap = new Map<Id, Profile>([SELECT Name FROM
Profile WHERE Id IN: profIds]);
List<Messaging.SingleEmailMessage> welcomeEmails = new
List<Messaging.SingleEmailMessage>();
List<EmailTemplate> welcomeEmailTemplates = [SELECT Id FROM EmailTemplate
WHERE DeveloperName = 'Taco_Bueno' LIMIT 1];
for(User usr: userList)
{
if(profilesFoundMap.get(usr.ProfileId).Name == PROFILE_NAME)
{
//Sending out our password reset emails
System.resetPassword(usr.Id, true);
}
//Setting up our welcome email to send by rendering our vf email
//template
Messaging.SingleEmailMessage welcomeEmailMessage =
Messaging.renderStoredEmailTemplate(welcomeEmailTemplates[0].Id,
usr.ContactId, null);
String [] toAddresses = new String[]{usr.Email};
welcomeEmailMessage.setToAddresses(toAddresses);
welcomeEmails.add(welcomeEmailMessage);
}
if(!welcomeEmails.isEmpty())
{
//Sending out our custom welcome emails
Messaging.sendEmail(welcomeEmails);
}
}
}
So again this code isn’t super complex, but let me explain what’s happening in the Community email manager just a bit.
1) Because we cannot get object relationship fields in triggers, if you are sending emails based on profiles we need to grab the ids of our new users profiles and query the profiles object to create a map of profiles so we can figure out the names of our users assigned profiles. Unfortunately we cannot use the Profile.Name relationship field on the User object like we did in our VF component controller.
2) If you created a custom welcome email that is separate from the password reset email, you need to query to find its template via the EmailTemplate object, we need the Id of the template to render it for our email we send out later in the code.
3) The System.resetPassword() method is what is sending out the reset password emails for our users. This will send out reset password emails for all communities they are members of (so be careful and make sure you only send them out when necessary).
4) The Messaging.renderStoredEmailTemplate method takes three parameters, the Id of the email template you intend to render, the Id of the recipientType and the Id of the RelatedToType for your template. This method works the best for rendering visualforce email templates.
5) We add our outbound emails to a list of single email messages. This is important because while we can send thousands of emails at one time, we can only call the Messaging.sendEmail() method up to ten times in the same context. So we want to send that method a giant list of emails instead of one email at a time.
Now, if you test out your emails you should have conditional rendering working in your templates, password reset links in your super customized password reset emails and now you can do whatever you can dream of with those emails. We did it!!!
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.
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.
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>
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.
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
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.