I have an object called Response_Documents_vgs__c . Requirement is, When this record is created, a mail should be sent to its owner means who created this record. I created an email template for this.

This is the html code of template

Dear {!Response_Documents_vgs__c.CreatedBy} <br/>

Your Response document is Created. You can download it from <a href="{!Response_Documents_vgs__c.Document_Location_vgs__c}"> here </a>

Now on after insert of response document, i am writing this code.

public static void sendEmail(List<Response_Documents_vgs__c> resps){
    List<EmailTemplate> template = [select Id,Name,Subject,body from EmailTemplate where name = 'EmailWhenResDocCreate'];
    List<Messaging.MassEmailMessage> emailList = new List<Messaging.MassEmailMessage>();
    List<ID> targetObjectIds = new List<ID>();
    if( !template.isEmpty()){
        for(Response_Documents_vgs__c doc : resps){

            Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
            mail.saveAsActivity = false;




        // Send the email

sendEmail is the method that i am calling from trigger with Trigger.new as parameter.

Now email is going to the user but it is


Your Response document is Created. You can download it from here "

means it is not having name after "Dear" and "here" is the simple text not hyperlink. The only problem is i dont know how to send the response document to template.

Any help regarding this would be great.

  • 1
    You need to set the WhatId so that the merge fields work correctly. If your trigger is on insert then the id will not be available.
    – BarCotter
    May 27, 2015 at 14:45
  • But according to this doc, The whatId values must be one of the following types: Contract Case Opportunity Product. How can i do it for custom object? May 27, 2015 at 14:49
  • I am used to using the SingleEmailMessage to send the emails, which allows the WhatId to be a custom object. Is there any reason you are using MassEmailMessage?
    – BarCotter
    May 27, 2015 at 14:56
  • When i am using SingleEmailMessage, i am getting this exception Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, sendEmailToOwner: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, WhatId is not available for sending emails to UserIds.: [] Class.sendEmailToOwnerLogic.sendEmail: line 20, column 1 Trigger.sendEmailToOwner: line 2, column 1: [] May 27, 2015 at 15:00
  • 1
    Any reason you can't just use a workflow for this? May 27, 2015 at 18:07

2 Answers 2


Please find the Email Template and the Trigger code. This sends an email when a record is created. Hope this will help.

Email Template - (Subjected - Record is created)

Dear {!CreatedBy} 
Your Response document is Created. You can download it from {!Document_Location_vgs__c}

Trigger Code

trigger SendEmail  on Response_Documents_vgs__c (after insert) {
    Response_Documents_vgs__c rdv= trigger.new[0]; 
    ID userid = rdv.CreatedById; 
    User activeUser = [SELECT Name,Email FROM User WHERE Id = :userid limit 1];
    String userName = activeUser.Name;
    String userEmail = activeUser.Email;
    String location = rdv.Document_Location_vgs__c;
    EmailTemplate emailTemplate = [SELECT Id, Subject, HtmlValue, Body FROM EmailTemplate WHERE Id = '00Xi0000000Kbba']; //Email Template's ID
    String htmlBody = emailTemplate.HtmlValue;
    htmlBody = htmlBody.replace('{!CreatedBy}', userName );
    htmlBody = htmlBody.replace('{!Document_Location_vgs__c}', location );
    String plainBody = emailTemplate.Body;
    plainBody = plainBody.replace('{!CreatedBy}', userName );
    plainBody = plainBody.replace('{!Document_Location_vgs__c}', location );

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setSenderDisplayName('Salesforce Support'); //Sender's Display name
    mail.setReplyTo('[email protected]'); //Sender's Email ID
    List<String> sendTo = new List<String>();
    Messaging.sendEmail(new Messaging.SingleEmailmessage[] {mail});     

Email Received

enter image description here


Please have a look into the page below.



You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .