while calling below code of sendEmail throwing an error as:
First error: SendEmail failed. First exception on row 0; first error: INVALID_FIELD_WHEN_USING_TEMPLATE, When a template is specified the plain text body, html body, subject and charset may not be specified : []
if I remove line - mail.setTemplateId(emailTemp.id);
below code works fine. Is there anything wrong I am doing?
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
EmailTemplate emailTemp=[Select Id,Subject, HtmlValue, body from EmailTemplate where name='Notify on oppy closed'];
List<String> sendTo = new List<String>();
for(opportunity_contact__c opContact : oppyContact) {
// sendTo.add(opContact.email__c);
mail.setSubject(emailTemp.Subject);
mail.setSenderDisplayName('Test Email');
mail.setHtmlBody(emailTemp.HtmlValue);
mail.setPlainTextBody(emailTemp.body);
mail.setTargetObjectId('0031100000YDHru');
mail.setSaveAsActivity(false);
mail.setWhatId(opContact.opportunity__c);
mail.setTemplateId(emailTemp.id); // #throwing error here
mails.add(mail);
}
Messaging.sendEmail(mails);
Custom Email Template :
Dear {!Opportunity_Contact__c.Name__c},
TEXT Here.....
TEXT Here.....
TEXT Here.....TEXT Here.....
Opportunity : {!Opportunity.Id}
Regards,
{!User.FirstName} {!User.LastName}
{!Organization.Name}
Thanks in advance!