You need to sign in to do that
Don't have an account?

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.: []
Suggestions?
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
mail.setToAddresses(toAddresses);
mail.setSubject(subject);
mail.setPlainTextBody(body);
//mail.setDocumentAttachments(docid);
mail.setTargetObjectId('003o0000005g1v8');
mail.setTemplateID('01Ho0000000TQmj');
mail.setSaveAsActivity(false);
// 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
inspectResults(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;
}
}
System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, Invalid template id.: []
Suggestions?
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
mail.setToAddresses(toAddresses);
mail.setSubject(subject);
mail.setPlainTextBody(body);
//mail.setDocumentAttachments(docid);
mail.setTargetObjectId('003o0000005g1v8');
mail.setTemplateID('01Ho0000000TQmj');
mail.setSaveAsActivity(false);
// 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
inspectResults(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;
}
}
Please comment following line in your code and try, you can either set the template or set the body manually not both.
mail.setPlainTextBody(body);
Regards,
Vivek Patel.
Please mark this as best answer if this solves your problem.
I commented out the 'mail.setPlainTextBody(body)' line of code and I still get the same error
Am I missing your point?
have u tried to query that template in Developer Console?
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?
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.
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?
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.