3

I have an Apex class that runs every ten minutes and sends emails for some opportunities that were created in the system. Or at least, that is how it should work, but I'm getting an INVALID_FIELD_WHEN_USING_TEMPLATE exception when sending them, even though I don't specify the fields that Salesforce says that are invalid.

The exception says the following:

When a template is specified, the plain text body, html body, subject, charset, and "treatBodiesAsTemplate" may not be specified

But I don't specify none of those in my code. I even put the email message in a debug line so I could see the parameters, and I get the following (formatted for better visualization):

Messaging.SingleEmailMessage[
getBccAddresses=(<my email addresses>);
getCcAddresses=null;
getCharset=null;
getDocumentAttachments=null;
getFileAttachments=null;
getHtmlBody=null;
getInReplyTo=null;
getOrgWideEmailAddressId=0D211000000CbbrCAC;
getPlainTextBody=null;
getReferences=null;
getTargetObjectId=00311000017I5y0AAC;
getTemplateId=00X11000000EDMOEA4;
getToAddresses=null;
getWhatId=0061100000C2ztMAAR;
isUserMail=false;
]

To clarify: I am using an email template for this, that "accepts" two Ids: an Opportunity Id and a Contact Id.

This is really frustrating. What can it be?

The code I'm using can be found at https://gist.github.com/renatoliveira/3795fce852f08260361e but I've changed some strings for anonymity.

The template is somewhat as follows (also redacted):

<messaging:emailTemplate subject="{!relatedTo.custom_field_b__c}"
    recipientType="Contact"
    relatedToType="Opportunity"
    replyTo="{!relatedTo.custom_field__r.Email}">
    <messaging:htmlEmailBody >
    <div class="maindiv" style="font-family: Arial, sans">
    Hello {!recipient.Salutation} {!recipient.FirstName},<br/><br/>

    [REDACTED]

    <br/><br/>You may get in touch with him/her <b><a href="mailto:{!relatedTo.custom_field__r.Email}">{!relatedTo.custom_field__r.Email}</a></b> or by phone (<b>[REDACTED]</b>).

    <br/><br/>Thank you for the opportunity and always count on us and on our support.

    <br/><br/>If you need further information, do not hesitate to contact us.

    <br/><br/>Best regards,<br/><br/>
    <b>[REDACTED]</b>

    </div>
    </messaging:htmlEmailBody>
</messaging:emailTemplate>
2
  • Try removing the BCC Addresses. I think the real problem may be combination of TargetObjectId with any address fields, despite what the message says.
    – Adrian Larson
    Feb 25, 2016 at 18:43
  • I can't do that. It is a requirement to send blind copies of the email to two persons. :( Feb 25, 2016 at 21:25

3 Answers 3

1

@AdrianLarson - I think the documentation is bogus as the following works fine:

Messaging.SingleEmailMessage email  = new Messaging.SingleEmailMessage();
email.setBccAddresses(new List<String> {'[email protected]'}); // set BCC
email.setCcAddresses(new List<String> {});
email.setToAddresses(new List<String> {});
email.setTargetObjectId([select id from Contact limit 1][0].id); // used for merge fields
email.setTemplateId([select id from EmailTemplate where name = 'Foo'][0].id);
email.setWhatId([select id from Foo__c limit 1][0].id); // used for merge fields
Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{email});

Renato - I would check your setup to the Messaging.SingleEmailMessage object and also verify that your email template uses merge fields of the form

Text/HTML templates

{!Contact.xxx} and {!Opportunity.yyy} (targetObjId and WhatId, resp.)

VF templates

{!recipient.xxx} and {!relatedTo.yyy} (targetObjId and WhatId, resp.)

6
  • That doesn't seem to be the problem. If I try to put "Opportunity.xxx" in the template, Salesforce throws me an error (Error: Unknown property 'core.email.template.EmailTemplateComponentController.Opportunity'). On the other side: how can I check the setup for the SEM object? Feb 26, 2016 at 21:23
  • Renato -- 1) what kind of email template is it? and 2) edit (use edit link) your post with the essence of your email template
    – cropredy
    Feb 26, 2016 at 21:27
  • It is a Visualforce Email Template. I also updated the post. Feb 26, 2016 at 21:38
  • @RenatoOliveira - in your gist, line 25 - I'm surprised this works as this should be sem.setSaveAsActivity(false)
    – cropredy
    Feb 26, 2016 at 21:56
  • 2
    Renato - I would approach this problem by trying to get something that works (minimal code) via execute anonymous with the simplest email template and then build up line by line to match your PROD code until you get the failure. This will be the clue. Your gist didn't include all of your methods so I couldn't do this for you
    – cropredy
    Feb 29, 2016 at 15:50
0

From the comments under @crop answer:

@RenatoOliveira - in your gist, line 25 - I'm surprised this works as this should be sem.setSaveAsActivity(false) – crop1645 Feb 26 at 21:56"

I think the error message is misleading. In the code posted above:

getTargetObjectId=00311000017I5y0AAC;
getTemplateId=00X11000000EDMOEA4;
getToAddresses=null;
getWhatId=0061100000C2ztMAAR;

If there's no activity record being created, then specifying a WhatId would be specifying an INVALID_FIELD_WHEN_USING_TEMPLATE if the template wasn't created for use with activity records.

I strongly suspect that if you remove the getWhatId=0061100000C2ztMAAR; line from your code, the error message will cease.

Since we've not seen the full template, it's difficult to give a definitive answer, but an activity would be related to the opportunity record Id, not a custom field on the opportunity record. This further causes me to believe this is the WhatId is source of the error message based on the information that's been provided.

0

Doing what everyone suggested didn't work for me at all. I appreciate the answers, though.

I must say that I really don't like Salesforce's documentation. This kind of thing (using the bbc field with a template) works, but the documentation says otherwise.

The solution on my way was to change the workflow, and remove this part of the automation. I am not sure why, but adapting the code to run in a button (the button would pass the Opportunity's Id to a webservice that would do the work of setting up the email and sending it) worked just fine.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .