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.

    . . .
    Subscribe Articles

    Just enter your email below to subscribe and receive weekly updates about the most interesting similar articles.

    You need to accept conditions to receive letters.
    You've Subscribed!

    Share article via

    . . .

    CustomObjectEmailExample Class

    Visualforce Page : sendEmail Page

    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