Skip to main content
replaced http://salesforce.stackexchange.com/ with https://salesforce.stackexchange.com/
Source Link

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 questionthe 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<Messaging.Email>, 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?

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<Messaging.Email>, 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?

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<Messaging.Email>, 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?

Tweeted twitter.com/#!/StackSalesforce/status/385243239944839168
Source Link
Jason Clark
  • 12.1k
  • 7
  • 64
  • 123

Messaging.sendEmail fails with TargetObjectId, WhatId, and SaveAsActivity

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<Messaging.Email>, 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?