+ Start a Discussion
Raj R.Raj R. 

How to send email using email template in apex class?

Hi,

I have an email template (templateId = 'xxxxx') that has custom fields which are part of the email tempalte. 

I have a trigger when when criteria A,b, and c are met, then an email is supposed to be sent. i am trying to use an email template while sending the email, but I am getting the follow error message below. Any thoughts as to what is causing the issue?

First exception on row 0; first error: INVALID_ID_FIELD, WhatId is not available for sending emails to UserIds.:

My email code logic is:
sendTemplatedEmail(Id whatId, Id targetObj, List<String> toAddr, Id templateId) {
        //create new SingleEmailMessage (i.e. eml)
        //setToAddress to toAddr
        //setTemplateId to templateId 
       //setTargetObjectId to targetObject
       //setWhatId to whatId
       //setSaveAsACtivity to false
      
        sendEmail(...);
    }

 
Best Answer chosen by Raj R.
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you

http://www.codespokes.com/2013/08/how-to-send-email-from-apex-code-with.html
http://sfdcsrini.blogspot.com/2014/06/how-to-get-salesforce-org-wide-address.html
http://opfocus.com/blog/sending-emails-in-salesforce-to-non-contacts-using-apex/
http://salesforce.stackexchange.com/questions/5169/whats-the-advantage-of-using-massemailmessage-instead-of-multiple-singleemailme/5174#5174
http://developer.force.com/cookbook/recipe/creating-email-templates-and-automatically-sending-emails
https://developer.salesforce.com/forums/?id=906F00000008zbSIAQ
 
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for(User portalUser :lstPortalUser) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
string body = 'Hi '+ portalUser.LastName;
mail.setSubject('Test Subject');
mail.setTargetObjectId(portalUser.Id); mail.setSaveAsActivity(false);
mail.setHtmlBody(body); mails.add(mail);
}
Messaging.sendEmail(mails);
// In a separate class so that it can be used elsewhere
Global class emailHelper {

public static void sendEmail(ID recipient, ID candidate) {

  //New instance of a single email message
 Messaging.SingleEmailMessage mail = 
            new Messaging.SingleEmailMessage();
 
// Who you are sending the email to
   mail.setTargetObjectId(recipient);

   // The email template ID used for the email
   mail.setTemplateId('00X30000001GLJj');
          
   mail.setWhatId(candidate);    
   mail.setBccSender(false);
   mail.setUseSignature(false);
   mail.setReplyTo('recruiting@acme.com');
   mail.setSenderDisplayName('HR Recruiting');
   mail.setSaveAsActivity(false);  
 
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

    }  
}
Please let us know if this will help you


 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you

http://www.codespokes.com/2013/08/how-to-send-email-from-apex-code-with.html
http://sfdcsrini.blogspot.com/2014/06/how-to-get-salesforce-org-wide-address.html
http://opfocus.com/blog/sending-emails-in-salesforce-to-non-contacts-using-apex/
http://salesforce.stackexchange.com/questions/5169/whats-the-advantage-of-using-massemailmessage-instead-of-multiple-singleemailme/5174#5174
http://developer.force.com/cookbook/recipe/creating-email-templates-and-automatically-sending-emails
https://developer.salesforce.com/forums/?id=906F00000008zbSIAQ
 
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for(User portalUser :lstPortalUser) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
string body = 'Hi '+ portalUser.LastName;
mail.setSubject('Test Subject');
mail.setTargetObjectId(portalUser.Id); mail.setSaveAsActivity(false);
mail.setHtmlBody(body); mails.add(mail);
}
Messaging.sendEmail(mails);
// In a separate class so that it can be used elsewhere
Global class emailHelper {

public static void sendEmail(ID recipient, ID candidate) {

  //New instance of a single email message
 Messaging.SingleEmailMessage mail = 
            new Messaging.SingleEmailMessage();
 
// Who you are sending the email to
   mail.setTargetObjectId(recipient);

   // The email template ID used for the email
   mail.setTemplateId('00X30000001GLJj');
          
   mail.setWhatId(candidate);    
   mail.setBccSender(false);
   mail.setUseSignature(false);
   mail.setReplyTo('recruiting@acme.com');
   mail.setSenderDisplayName('HR Recruiting');
   mail.setSaveAsActivity(false);  
 
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

    }  
}
Please let us know if this will help you


 
This was selected as the best answer
Vijay NagarathinamVijay Nagarathinam
Hi Amit,

I tried the same that you are mentioned above. But I am getting the static content in the email, I have used some merge fields in the template. That fields are not working. Can you please help me?

Thanks,
Vijay
Pooja Gupta 54Pooja Gupta 54
@vijay

You can try by hardcoding the id of the record whose merge fields are being referenced in the email. ex- mail.setWhatId('id').
You will see the merge fields being populated in the email, if it works you can then make it dynamic.