Menu Close
    Hire Us Request Quote Reading List Switch to Dark Mode

    Send Merge Field Of Custom Object Through Email Template Using Apex

    If you want to send merge fields of a custom object from Email template via apex you must have to follow the following steps in below.

    1. Create a Custom Object and Relationships fields

    You can Create fields according to your requirement but at least one field must be Lookup or Master-detail relationship because Salesforce doesn’t allow to replace the merge field from the template so we need to create this relationship field which is described in below because Only Contact, Lead, or Person objects are allowed for targetObjectId.



    2. Create Records of Custom Object

    You have to create records of that object. eg: sample record

    3. Create Email Template with Merge Fields of Custom Object

    Go to Setup -> Email-> Classic Email Template
    create a new Email template and write the merge field of the custom object according to your need. eg: Sample

    4. Create Apex Class to send Email

    Here I am creating an apex class CustomObjectEmailExample which will be called from Visualforce page. You can utilize it according to your business scenario.

    CustomObjectEmailExample Class

      * Webkul Software.
      * @category  Webkul
      * @author    Webkul
      * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (
      * @license
    public class CustomObjectEmailExample {
    	public PageReference sendingMail() {		
            Messaging.SingleEmailMessage mail =  new 
            Messaging.SingleEmailMessage();// create instance of 
             SingleEmailMessage class
    	String[] sendingToBccAdd = new String[] 
            {''}; // Add email addresses to blind 
            carbon copy (BCC) addresses. The maximum allowed is 25.
           String[] sendingTocAdd = new String[]{''};  
           // add addresses to carbon copy(CC). The maximum allowed is 25.
            mail.setTargetObjectId('xxxxxxxxxxx');// This is the ID of rel 
            mail.setWhatId('XXXXXXXXXXXXXXXXX');// This is the record ID
            mail.setTemplateId(template);// This is template ID
    	return null;

    Visualforce Page : sendEmail Page

      * Webkul Software.
      * @category  Webkul
      * @author    Webkul
      * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (
      * @license
    <apex:page controller="CustomObjectEmailExample">
      <apex:form >
        <apex:commandButton value="Send Email" action="{!sendingMail}"/>

    5. Output of Example


    If you want to send mail through existing object which has a large number of records then you can create relationship (lookup/master) and associate with Contact or Lead and you can use flow or batch apex by matching email field of custom object with contact or lead to populate this field because Email of Contact or Lead will be treated as a recipient.

    Note: We can create the maximum number of lookup fields is 25 and master-detail field is 2 on a custom object


    If you have any issue feel free to add a ticket and let us know your views to make it better

    Supported Framework Version - 45

    . . .


    Add Your Comment

    Be the first to comment.

    Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again