2

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){
            targetObjectIds.add(doc.createdById);

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

            mail.setTargetObjectIds(targetObjectIds);
            mail.setTemplateId(template[0].Id);

            mail.setUseSignature(false);
            mail.setSaveAsActivity(false);

            emailList.add(mail);

        }
        // Send the email
        Messaging.sendEmail(emailList);
    }
}

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

"Dear

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 '15 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? – Sachin Kadian May 27 '15 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 '15 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: [] – Sachin Kadian May 27 '15 at 15:00
  • 1
    Any reason you can't just use a workflow for this? – Todd Sprinkel May 27 '15 at 18:07
-1

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;
    System.debug(rdv.Document_Location_vgs__c);
    System.debug(userEmail);
    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('support@salesforce.com'); //Sender's Email ID
    mail.setUseSignature(false);
    mail.setBccSender(false);
    mail.setSaveAsActivity(false);
    mail.setSubject(emailTemplate.Subject);
    mail.setHtmlBody(htmlBody);
    mail.setPlainTextBody(plainBody);
    List<String> sendTo = new List<String>();
    sendTo.add(userEmail);
    mail.setToAddresses(sendTo);       
    Messaging.sendEmail(new Messaging.SingleEmailmessage[] {mail});     
}

Email Received

enter image description here

-1

Please have a look into the page below.

http://www.sfdc99.com/2014/03/01/sending-emails-using-apex/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.