+ Start a Discussion
Mark RutterMark Rutter 

Having trouble with .setTemplateID

I am experimenting with sending emails with attachments and get the this error message when I run the following code:
System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, Invalid template id.: []


public class EmailManager {

    // Public method
    public void sendMail(String address, String subject, String body) {
        // Create an email message object
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {address};
        //List<String> docid=new List<String>{'015o00000017GD1'}; // Give the document ID here
        // Pass this email message to the built-in sendEmail method 
        // of the Messaging class
        Messaging.SendEmailResult[] results = Messaging.sendEmail(
                                 new Messaging.SingleEmailMessage[] { mail });
        // Call a helper method to inspect the returned results
    // Helper method
    private static Boolean inspectResults(Messaging.SendEmailResult[] results) {
        Boolean sendResult = true;
        // sendEmail returns an array of result objects.
        // Iterate through the list to inspect results. 
        // In this class, the methods send only one email, 
        // so we should have only one result.
        for (Messaging.SendEmailResult res : results) {
            if (res.isSuccess()) {
                System.debug('Email sent successfully');
            else {
                sendResult = false;
                System.debug('The following errors occurred: ' + res.getErrors());                 
        return sendResult;

Hi Mark,

Please comment following line in your code and try, you can either set the template or set the body manually not both.


Vivek Patel.

Please mark this as best answer if this solves your problem.
Mark RutterMark Rutter
Thank you.

I commented out the 'mail.setPlainTextBody(body)' line of code and I still get the same error
Go to Template -> tick the checkbox "Available for Use"
Paul S.Paul S.
Just a guess here, but if there's a body parameter in your sendMail method is that interfering with the use of the email template?
Mark RutterMark Rutter
Thought that was is, but my template is a Word mail merge template.  There is to 'Available for Use' checkbox on these templates.  There is for email templates.
Mark RutterMark Rutter
Yes Paul, but if I comment out the 'mail.setPlainTextBody(body)' line of code, I still get the same error message.
Am I missing your point?
i doubt whether we can use word mail merge template in Email. It should be Email Template like HTML, plain text or VF.

have u tried to query that template in Developer Console?
Mark RutterMark Rutter
OK, thanks to you all.  It looks like I may be going down the wrong path.

What I want to do is to attach a mail merge document as a PDF file to a list of contacts.  Any suggestions on how I should approach this? 
Paul S.Paul S.
Hi Mark - just to confirm, were you trying to use the template as the email itself or as a template for the attachment to the email?
Mark RutterMark Rutter
The template is a Word mail merge template in Communication Templates.  This is just a simple test document that merges the Contacts first and last names.
I then want to generate an email with this merged document as an attachment - not as the body of the email itself.
Paul S.Paul S.
Ah, OK. Try putting together a simple email template and use that in the setTemplateID method to see if you can at least get the email to send. I don't have much experience with attachments, but I have created simple Visualforce pdf documents within the UI. I'm guessing, then, that creating and attaching such a document to these emails would be possible. Found this as well. It's a bit over my head, frankly, but it does seem to prove what you want to do is possible. http://blog.jeffdouglas.com/2010/07/16/create-and-email-a-pdf-with-salesforce-com/
Mark RutterMark Rutter
Thanks Paul. I am now trying to take the Visualforce path.  The link you provided looks helpful.
I have put a VF page together with a custom controller in an attempt to accomplish what I want.  Question: I want to be able to create a custom button on Accounts that will 'call' the VF page, but the only Pages available in Content are those where the controller is Account.  
So, if my VF page starts with <apex:page controller="PdfEmailController"> , how to I get to that page with a custom button on Accounts?
Paul S.Paul S.
I do have a custom button that creates a pdf document on my lead object.  In my case I just used the standard lead controller and rendered the page as a pdf.
<apex:page standardController="Lead" renderAs="pdf">

What I'm not sure about is if you'd then be able to take such a page and use it also as an email attachment.