I have the following code

if (this.templateId != null)  {
           // parameters specific for visualforce template
           } else {
return this; 

The message body has a link and if I set the whatID and setSaveAsActivity(true); the email is not being sent.

If I remove the whatID the setSaveAsActivity(true); then the email doesn't send the proper link.

I am very new to apex coding and any help will be appreciated

  • What object is the targetObjectId: contact, lead or user? What object is the WhatId? Commented Sep 13, 2015 at 16:54

1 Answer 1


From the Apex Code Developer's Guide:


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.

The above method relies on you using setTargetObjectIds, to set the WhoID(s) for your email message. If you set the targetobjectIds as anything other than contacts or leads, then you'll have a problem because it creates a Task that's assigned on that Contact or Lead that can also be related to a 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.


The value must be one of the following types:

  • Account
  • Asset
  • Campaign
  • Case
  • Contract
  • Opportunity
  • Order
  • Product
  • Solution
  • Custom

You must log in to answer this question.

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