Reading list Switch to dark mode

    Send Merge Field Of Custom Object Through Email Template Using Apex

    Updated 2 August 2019

    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.

    eg:

    CustomObject

    image unavailable

    2. Create Records of Custom Object

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

    image unavailable

    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

    Searching for an experienced
    Salesforce Company ?
    Find out More
    image unavailable

    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 (https://webkul.com)
      * @license   https://store.webkul.com/license.html
      */
    public class CustomObjectEmailExample {
    	public PageReference sendingMail() {		
            Messaging.SingleEmailMessage mail =  new 
            Messaging.SingleEmailMessage();// create instance of 
             SingleEmailMessage class
    	String[] sendingToBccAdd = new String[] 
            {'[email protected]'}; // Add email addresses to blind 
            carbon copy (BCC) addresses. The maximum allowed is 25.
           mail.setBccAddresses(sendingToBccAdd);
           String[] sendingTocAdd = new String[]{'[email protected]'};  
           // add addresses to carbon copy(CC). The maximum allowed is 25.
    	mail.setCcAddresses(sendingTocAdd);
            mail.setReplyTo(userinfo.getUserEmail());
            mail.setTargetObjectId('xxxxxxxxxxx');// This is the ID of rel 
            mail.setSaveAsActivity(false);
            mail.setWhatId('XXXXXXXXXXXXXXXXX');// This is the record ID
            mail.setTemplateId(template);// This is template ID
            mail.setSenderDisplayName(userinfo.getname());
            Messaging.sendEmail(mail);
    	return null;
    	}
    }

    Visualforce Page : sendEmail Page

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

    5. Output of Example

    image unavailable

    Conclusion

    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

    Support

    If you have any issue feel free to add a ticket and let us know your views to make it better https://webkul.uvdesk.com/en/customer/create-ticket/

    Supported Framework Version - 45

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home