function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Malakai LaviMalakai Lavi 

Send EMail exception- INVALID_FIELD_WHEN_USING_TEMPLATE

I have some code in a class that is called by a triffer that sends emails. I am intermittently receiving exceptions when the code is called. The exceptions only happen about 10% of the time or less (Making it very difficult to set up debug logging for a number of users to try and perhaps catch when the exception occurs). I can't work out what is causing the exception as the error message does not make sense in context of the code.

Exception caused by: System.EmailException: 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 : []

The code on the line specified in the exception:
//Add emails to the list of emails to be sent
                                  Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                                  message.setOrgWideEmailAddressId(emailAddress.Id);
                                  message.setTargetObjectId(c.ContactId);
                                  message.setWhatId(c.Id);
                                  message.setTemplateId(inttemplate.Id);
                                  messages.add(message);
                          }
Messaging.sendEmail(messages);
                     
Clearly it doesn't specify plain text body, html body, subject or charset.
Best Answer chosen by Malakai Lavi
pconpcon
Great to hear you got it fixed!

My guess is that when you call sendEmail on a list of messages with a template Id it is filling out the templates field on the message object.  Then on the second pass it causes the internal mechanisms to try to reset those fields again, but throws the exception because now both the template id and the fields are set which it doesn't like.

All Answers

pconpcon
Is it possible that this code is being called multiple times (and that messages is a static variable)?  If that is the case then it could be complaining because the singleEmailMessage is already populated from a previous call.
Malakai LaviMalakai Lavi
Thanks pcon.

I did try to change the name of the variable, but that was not the cause of the issue in this instance. I have made a change and I think it has worked.
I was able to replicate the issue (and hence get the debug logs) and the problem only happened when someone tried to do multiple actions at once (which would cause multiple emails to be sent).

Here is part of the debug log, as you see subject and plainTextBody are now specified (not sure if it is relevant at all).

1 15:55:42.059 (1059108536)|SYSTEM_METHOD_ENTRY|[80]|Messaging.sendEmail(LIST<Messaging.Email>)
2 15:55:42.417 (1417591299)|EMAIL_QUEUE|[80]|subject:" *****", bccSender: false,  saveAsActivity: true, useSignature: true, targetObjectId: ******************, whatId: ******************, templateId: ******************, plainTextBody: "***********"

My problem was I had the Messaging.sendEmail line in the wrong place. It was within the block of code that added emails to the list, so it was sending each email as it was added instead of in bulk which may have caused it to try and send multiple emails in every loop. When it got to the second loop through, it would throw the exception.

Now that I have changed the sendEmail line to the correct place the exception does not get thrown.

I am not sure why this has fixed the issue. If anyone could shed some light, it would be appeciated.
pconpcon
Great to hear you got it fixed!

My guess is that when you call sendEmail on a list of messages with a template Id it is filling out the templates field on the message object.  Then on the second pass it causes the internal mechanisms to try to reset those fields again, but throws the exception because now both the template id and the fields are set which it doesn't like.
This was selected as the best answer
Molson94Molson94
Hey Malakai,

I am running into a very similar issue on my end. I posted my question here:
https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Apex_Code_Development&criteria=OPENQUESTIONS&id=906F0000000AzXFIA0

In my log, the list of messages has the above noted error fields as "null".
Was that the case for you as well?