function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
carlocarlo 

Send email to Lead related to campaign and save as activity

Hello

 

I am trying to send an email to a Lead using a Template.  I want to store the email as an activity.

 

I have created a template.  It is a VF template with recipientType Lead and relatedToType Campaign.  When I Send Test and Verify Merge Fields this works fine.  It asks me for the Lead/LeadId and the Campaign/CampaignID.  Then if I send the email using Apex if Save as Activity is false it works fine but if Save as Activity is true it does not.  I want to Save the Activity so I can see which Leads were sent which emails - standard CRM stuff.

 

This works fine:

 

mail.setTargetObjectId(LeadId);

mail.setSaveAsActivity(false);

mail.setTemplateID(TemplateId);

mail.setWhatID(CampaignId);

 

This throws the error:

 

mail.setTargetObjectId(LeadId);

mail.setSaveAsActivity(true);

mail.setTemplateID(TemplateId);

mail.setWhatID(CampaignId);

 

Error is:

 

System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, WhatId is not available with saveAsActivity for sending emails to Leads.

 

So the error is pretty self explanatory.  I don't need an explanation of this error.  I want to know how I can work around this if possible.  

 

Simply I want to send an email to a Lead which is related to a campaign [uses merge fields] and save this as an activity.

 

Thanks

 

Carlo

 

 

sfdcfoxsfdcfox

Don't use setWhatId in this case. You cannot specify a WhatId with a Lead WhoId. Why? Because it's a limitation of the platform. You can either create a secondary task assigned to the campaign, or just not bother at all. Take a look at the Task standard object in the Web Services API Developer's Guide for details.