I see several things happening here.
First, there's an issue with the following:
setCcAddresses(ccAddresses)
Optional. A list of carbon copy (CC) addresses. The maximum allowed is 25. This argument is allowed only when a template is NOT used.
As you're using a template, this obviously causing an issue for you.
Next, it sounds as though you want your email to be about your customer contact, in which case, the contact needs to be the Id you use for:
`setTargetObjectId(targetObjectId)`
Required if using a template, optional otherwise. The ID of the contact
, lead
, or user
to which the email will be sent. The ID you specify sets the context and ensures that merge fields in the template contain the correct data.
You'll note that the above can't be anything other than a one of those three.
For your purposes, you may need to also use this field setting it to false:
setTreatTargetObjectAsRecipient(treatAsRecipient)
Optional. If set to true, the targetObjectId (a contact, lead, or user) is the recipient of the email. If set to false, the targetObjectId is supplied as the WhoId field for template rendering but isn’t a recipient of the email. The default is true.
setWhatId(whatId)
If you specify a contact for the targetObjectId field, you can specify an optional whatId as well. This helps to further ensure that merge fields in the template contain the correct data.
Since you had wanted to reference c.CustomerSignedId
, it sounds as though you really want to reference some kind of contract or document that's been signed, if so, that's probably what needs to go here for the WhatId
.
The value for the WhatId
must be one of the following types:
- Account
- Asset
- Campaign
- Case
- Contract
- Opportunity
- Order
- Product
- Solution
- Custom
If you want to include yourself as the admin, instead of adding yourself to the cc's or BCC's, you can always just add yourself to the list of email address your sending the email to. However, with a template, you can't do it using any of the below:
setToAddresses(toAddresses)
setCcAddresses(ccAddresses)
setBccAddresses(ccAddresses)
Optional. A list of email addresses to which you are sending the email. The maximum number of email addresses allowed is 100. This argument is allowed only when a template is NOT used.
Instead, using a template, if you want to use set
for the addresses, you're going to need to use either an HTML or text email body tag:
setHtmlBody(htmlBody)
Optional. The HTML version of the email, specified by the sender. The value is encoded according to the specification associated with the organization. You must specify a value for setTemplateId, setHtmlBody, or setPlainTextBody. Or, you can define both setHtmlBody and setPlainTextBody.
setTreatBodiesAsTemplate(treatAsTemplate)
After doing the above, I suggest you try using this tag if you have merge fields you want to use. It's possible it may work for you without throwing an error. I've not tested it to see, but it's worth a try.
setTreatBodiesAsTemplate(treatAsTemplate)
Optional. If set to true, the subject, plain text, and HTML text bodies of the email are treated as template data.
For more on all the above, see SingleEmailMessage Methods
Following link has an answer to the question: Using APEX to assemble HTML Letterhead Emails
Summer 16 introduced new method:
Messaging.SingleEmailMessage email =
Messaging.renderStoredEmailTemplate(templateId, whoId, whatId);
Instead of using Messaging.SingleEmailMessage email=new SingleEmailMessage();
we have to use
Messaging.SingleEmailMessage email =
Messaging.renderStoredEmailTemplate(templateId, whoId, whatId);
This will take care of retrieving the body and merging the field in VF email templates
Best Answer
I'm going to try to hit on several things here that may or may not prove fruitful for you. First, from the Winter 14 Apex Code Developer's Guide:
Are your recipients valid contact ID's? I'm assuming they are and that when you tested, you set the
IsActive
flag on them totrue
.I notice you're using a VisualForce template. Have you attempted resolving your problem by using an HTML or Text based template instead? Your problem could easily be related to a problem in your VisualForce that wouldn't at all be apparent.
Another thing that may be of relevance to your issue is the following:
Have you enabled Shared Activities for your org? If not, that could be at the root of the issue you're having. In essence, the
TargetObjectid
is theWhoID
and theRelatedToType
is theWhatID
of your email/activity. I see from what you've posted above that you've specified both theRelatedToType
and theWhatID
. That in itself may very well be the actual source of your problem.Finally, if this doesn't help you resolve your issue, I'd highly recommend you post a copy of the gist of your template that includes all of the fields (please remove any confidential or identifying information). Its possible there's something about a field you're using in it that's creating a conflict which isn't readily obvious.