Tag Archives: Email Template

Custom Send Email Button on Custom Objects

  • Go to Setup | Create | Objects.
  • Select your Custom Object.
  • Go to the Custom Buttons, Links and Actions section.
  • Click on New Button or Link.
  • Create a New Custom Button.
  • Select the Display Type as Detail Page Button.
  • Select the Behavior as Execute JavaScript.
  • Select the Content Source as OnClick JavaScript.
  • Include below code:

window.location = '/_ui/core/email/author/EmailAuthor?rtype=003&p3_lkid={!CustomObject__c.Id}&retURL=/{!CustomObject__c.Id}&p5={!$User.Email}&p24="{!CustomObject__c.To_Email__c}&template_id=00X90000001FCj4';

Note: If the custom object holds some of the contact details then you would need to use the values from custom object, not the contact object.

Save it. Edit the Page Layout of the Custom Object and drag this button on to the Page Layout under the Custom Button section.

The information shown below are the parameters that can be added in the URL. 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

Using Custom Controller in Visualforce Email Template

To show some records or render customized content on a visualforce email template, we need to include a custom component in a Visualforce email template that uses the custom controller.

Here is an example of visualforce email template with list of contacts of one account record.

Apex Controller:

public class AccountEmailTemplate
    public Id accountId {get;set;}
    public List<Contact> getContactList()
        List<Contact> conList;
        conList = [SELECT FirstName, LastName, Email, Phone FROM Contact WHERE AccountId =: accountId];
        return conList;

Visualforce Component(ContactList):

<apex:component controller="AccountEmailTemplate" access="global">
    <apex:attribute name="AccId" type="Id" description="Id of the account" assignTo="{!accountId}"/>
    <table border = "2" cellspacing = "5">
            <td>First Name</td>
            <td>Last Name</td>    
        <apex:repeat value="{!ContactList}" var="con">

Visualforce Email Template:

<messaging:emailTemplate subject="List of Contacts" recipientType="User" relatedToType="Account">
    <messaging:htmlEmailBody >
        Below is the list of contacts for account {!relatedTo.Name}.<br/><br/>
        <!--Embedded Visualforce component here -->
        <c:ContactList AccId="{!relatedTo.Id}" /><br/><br/>