I'm trying to send email from Apex using a VF Email Template. The <messaging:emailTemplate> tag in the template specifies recipientType='contact' relatedToType='MyCustomObj__c'.

I'm using code taken from the answer to this question, basically verbatim:

  1. create a new Messaging.singleEmailMessage()
  2. setTargetObjectId(a valid contactid)
  3. setWhatId(Id of a MyCustomObj__c)
  4. setTemplateId(id of my VF template)
  5. setSaveAsActivity(true)
  6. Messaging.sendEmail(emails, false)

The only difference in my code is that I repeat steps 1-5 to create a list, add each message to the list after step 5, and save them all at once in step 6. This fails every time with message: Messaging.SendEmailError[getTargetObjectId=null]. If I instead setSaveAsActivity(false), it succeeds. I have 'Enable Email Tracking' checked in Activity Settings in my org. Is there another setting I need to enable email tracking of mail sent to contacts? Can I not SaveAsActivity when using a Visualforce email template? I can't find any explicit prohibition. Something else?

  • Is it possible that one of the records in the list doesn't have a contact Id? From setSaveAsActivity(Boolean) docs "This argument only applies if the recipient list is based on targetObjectId or targetObjectIds". So if the TargetObjectId was missing on any of the SingleEmailMessage records it could explain the error. Commented Oct 2, 2013 at 0:38
  • @DanielBallinger: No. My test is creating all data, and sending two emails; I'm logging every value going into them. Both contactids are non-null, valid contacts. Commented Oct 2, 2013 at 0:42
  • Maybe you need to use MassEmailMessage with setTargetObjectIds(ID[]) if you want to do multiple records and save activities? Commented Oct 2, 2013 at 0:45
  • I'm sending multiple individual emails - part of a scheduled job to sent notifications to contacts about upcoming events - so MassEmailMessage isn't appropriate. Commented Oct 2, 2013 at 0:47
  • OK, I have seen a few posts with exactly the same error message. In those cases one of the Contacts was missing the email address. A bit misleading, and I'm not sure why changing setSaveAsActivity() would change it, but worth a quick check. Commented Oct 2, 2013 at 1:06

4 Answers 4


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:


Optional. The default value is true, meaning the email is saved as an activity. This argument only applies if the recipient list is based on targetObjectId or targetObjectIds. If HTML email tracking is enabled for the organization, you will be able to track open rates.


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.


public Void setTargetObjectId(ID targetObjectId)



Type: ID

Return Value Type: Void


Do not specify the IDs of records that have the Email Opt Out option selected. All email must have a recipient value of at least one of the following:

• targetObjectIds

• toAddresses

• ccAddresses

• bccAddresses

• targetObjectId

Are your recipients valid contact ID's? I'm assuming they are and that when you tested, you set the IsActive flag on them to true.


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.

Email Template

A form email that communicates a standard message, such as a welcome letter to new employees or an acknowledgement that a customer service request has been received. Email templates can be personalized with merge fields, and can be written in text, HTML, or custom format.

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:

EventRelation allows a variable number of relationships, as follows:

If you’ve enabled Shared Activities for your organization, an event can be related to up to 50 contacts or one lead. If you haven’t enabled Shared Activities, an event can be related to only one contact or lead.

An event can also be related to one account, asset, campaign, case, contract, opportunity, product, solution, or custom object.

TaskRelation allows a variable number of relationships, as follows:

A task can be related to one lead or up to 50 contacts.

A task can also be related to one account, asset, campaign, case, contract, opportunity, product, solution, or custom object.

Available in API versions 24.0 and later. Available only if you’ve enabled Shared Activities for your organization.

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 the WhoID and the RelatedToType is the WhatID of your email/activity. I see from what you've posted above that you've specified both the RelatedToType and the WhatID. 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.


Check your logs and you will find System.EmailException: SaveAsActivity is not allowed with whatId that is not supported as a task whatId.: []

It means that if you delete setWhatId() then your email will attach as activity to contact.


Your question is answered here: SaveAsActivity is not allowed with whatId that is not supported as a task whatId.


This error can be thrown if your relatedTo object in your template doesn't have the "Allow Activities" option checked. Checking this option will solve this issue.


My2cents : if you setWhatId, it seems that you can't setSaveAsActivity to true Cheers

  • 2
    Do you know of any documentation to support this?
    – Matt Lacey
    Commented Jul 24, 2014 at 2:45

You must log in to answer this question.

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