This tutorial will walk you through the steps of overriding the Salesforce “Send an Email” button and show you how to pre-populate the “To” field, subject, and template selected from fields on the related record.
There may be times where you need to pre-populate the “To” field from the Send an Email button. This is particularly the case with Custom Objects when it doesn’t know which contact to send the email to. By overriding the Send an Email button, you can pre-populate the To field with any Contact field from the related object.
Use the standard “Send an Email” button in the Activity History to redirect you to the Email Message page.
In this example, the parent record is an Opportunity and the URL of this page is:
If we want to auto-populate the other fields we need to copy this portion of the URL(up to the question mark):
For this example, we will be replacing the standard “Send an Email” button in the Opportunity’s Activity History related list.
Create a new Send an Email button
Go to setup >> Search for “Activities” keyword >> Task Buttons, Links, and Actions
Paste the URL portion we got from the standard email message.
The information shown below are the parameters that can be added in the URL we copied. Fields that are mark with asterisk are always needed in the URL.
|To (can be Contact or Lead Id)
|*Related To (usually the parent record Id)
|Salesforce email template Id
|*Redirection page when cancel button is clicked
In this example, we created a Contact lookup field in the Opportunity object for “p2_lkid” parameter and copied the Id of a sample email template for the “Template_Id” parameter.
You can get the Id of the email template by going to Setup >> Search for “Email” keyword >> Email Templates
You can find the template Id as highlighted in the URL below:
We are now ready to add the parameters to the URL.
Use the “&” as a separator for every parameter:
Save your “Send an Email” Custom button and add it to your page layout in the Activity History related list as shown below:
Add the new button to the Page Layout
In the Buttons section uncheck the standard “Send an Email” button, add the custom button we created, and save the page layout.
Here is the output of the custom button we created once clicked, it auto-populates the fields we added in the URL.