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
Renato SoluRenato Solu 

Invalid fields on single email message, even with the fields not being set

I'm having an issue where Salesforce raises me an exception while trying to send three email messages (using the SingleEmailMessage class).

The exception code is INVALID_FIELD_WHEN_USING_TEMPLATE and it says When a template is specified, the plain text body, html body, subject, charset, and "treatBodiesAsTemplate" may not be specified. It would be a pretty straightforward thing to solve, however I am not setting any of those fields in my code.

In my code, I basically get a Contact Id, an Opportunity Id, and the Opportunity Owner's email address to send the email to. I am setting the email as BCC. Do notice though that I have created similar code to other classes in the organization, and they work just fine (setting a single email message with a template and bcc field).

If it is of any help, I also posted the question to Salesforce StackExchange.
@Karanraj@Karanraj
Renato,

As per the documentation, you can't set the BCC addresses when your using template in your email message.
setBccAddresses(bccAddresses)
Optional. A list of blind carbon copy (BCC) addresses. The maximum allowed is 25. This argument is allowed only when a template is not used.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm
Renato SoluRenato Solu
Have you even read my message? I said I do this in other classes in my system (that means setting the bcc field) and it works just fine. The documentation is wrong about this. Even the guy who replied to my question on SE said that the documentation is bogus about this.