Overview:
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.
Use case:
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.
Steps:
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:
https://na22.salesforce.com/_ui/core/email/author/EmailAuthor?p3_lkid=0011500001DMqCD&retURL=%2F0011500001DMqCD
If we want to auto-populate the other fields we need to copy this portion of the URL(up to the question mark):
/_ui/core/email/author/EmailAuthor?
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.
Parameter Name | Parameter Value |
p2_lkid | To (can be Contact or Lead Id) |
p3_lkid | *Related To (usually the parent record Id) |
p4 | CC |
p5 | BCC |
p6 | Subject |
p23 | Email Body |
p24 | Additional To |
Template_Id | Salesforce email template Id |
retURL | *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:
/_ui/core/email/author/EmailAuthor?
p3_lkid={!Opportunity.Id}&
p2_lkid={!Opportunity.ContactId__c}&
template_id=00X15000001fVjr&
retURL={!Opportunity.Id}
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.