+ Start a Discussion
Aurélien.LavalAurélien.Laval 

INVALID_FIELD_WHEN_USING_TEMPLATE in a batch

Hello all,

I'm developping a batch which send emails to a contact List but it doesn't work and I'm getting all time this error :
[code]
INVALID_FIELD_WHEN_USING_TEMPLATE, When a template is specified the plain text body, html body, subject and charset may not be specified : []
[/code]

And below, my code :
[code]
List<EmailTemplate> emailTemplateList = [
      SELECT Id
      FROM EmailTemplate
      WHERE DeveloperName = :emailTemplateName
];

List<Messaging.SingleEmailMessage> emailsToSend = new List<Messaging.SingleEmailMessage>();

for(Contact aContact : contactList){
                        
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                            
      mail.setTargetObjectId(aContact.Id);
                        
      mail.setOrgWideEmailAddressId(oweaMap.get(emailMap.get(aRegion)).Id);
                    
      mail.setSubject(mailSubject);
                        
      mail.setSaveAsActivity(false);
                        
      mail.setBccSender(false);
                        
      mail.setUseSignature(false);
                        
      mail.setCharset('UTF-8');
                                                
      mail.setTemplateId(emailTemplateList[0].Id);
                        
      emailsToSend.add(mail);
}

Messaging.sendEmail(emailsToSend);
[/code]

The error is on the last line : Messaging.sendEmail(emailsToSend);

I've trying in selecting Body, HtmlValue and Subject field on the EmailTemplate object but it doesn't work..

What's wrong please?
Best Answer chosen by Aurélien.Laval
Aurélien.LavalAurélien.Laval
I've fixed my problem.

This is my code :
List<Messaging.SingleEmailMessage> emailsToSend = new List<Messaging.SingleEmailMessage>();

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
							
mail.setTargetObjectId(aContact.Id);
							
mail.setOrgWideEmailAddressId(oweaMap.get(emailMap.get(aRegion)).Id);
						
mail.setSaveAsActivity(false);
							
mail.setTemplateId(emailTemplateList[0].Id);
							
emailsToSend.add(mail);

 

All Answers

siddarth rajsiddarth raj
Test Email template directly from the page. It would be more easier to identify the if there is any problem in template
Aurélien.LavalAurélien.Laval
My email template is very simple :
Dear {!Contact.Name},

We just wanted to send you a Happy Birthday wish, from all of us.

May you celebrate it with those most meaningful to you,

Sincerely,
I've teste the email template in sending myself an email and it works.

 
Aurélien.LavalAurélien.Laval
I've fixed my problem.

This is my code :
List<Messaging.SingleEmailMessage> emailsToSend = new List<Messaging.SingleEmailMessage>();

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
							
mail.setTargetObjectId(aContact.Id);
							
mail.setOrgWideEmailAddressId(oweaMap.get(emailMap.get(aRegion)).Id);
						
mail.setSaveAsActivity(false);
							
mail.setTemplateId(emailTemplateList[0].Id);
							
emailsToSend.add(mail);

 
This was selected as the best answer