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
colemabcolemab 

Use lead to populate email template but send to different email address?

Everyone,

 

I have some code setup to send a confirmation email upon creation of lead (via the controller extension and not via a trigger).   I use the setTargetObjectID method to map this to the lead so that the template can be populated from the lead data.

 

However, I don't want to email the contact on the lead.  Instead I want to send the email to a custom email field on the lead.

 

The documentation (see the page here) seems to indicate this is possible when it says:

"All emails must have a recipient value in at least one of the following:

toAddresses, ccAddresses, bccAddresses, targetObjectId, targetObjectIds"

 

I set the toaddress in the code and it works as long as I don't use the setTargetObjectID and setTemplateID methods (i.e. I hard code the email).

 

The error I get when I try to use the object and template (even though I am setting the toaddress before and after) is:

"VF_PAGE_MESSAGE Error: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, no email found for id ".

 

So it seems to require me to send the email to the email address on the lead object - even though I have set the address explicetly.  Has anyone discovered a work around for this issue?

 

Here is an edited for public consumption copy of the relevant code:

 

Messaging.reserveSingleEmailCapacity(1);

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
                
String[] toAddresses = new String[ {CurrentLead.Email_Confirmation_Address__c};

email.setToAddresses(toAddresses);

email.setTargetObjectId(this.CurrentLead.id);
email.setSaveAsActivity(false);
                
EmailTemplate et = [SELECT Id FROM EmailTemplate WHERE DeveloperName = 'LeadConfirmation' LIMIT 1];				 

email.setTemplateId(et.id);

Messaging.SingleEmailMessage[] EmailsToSend = new Messaging.SingleEmailMessage[] { email };
                
Messaging.sendEmail(EmailsToSend);

 

Thanks for any help!

colemabcolemab

*Bump* -  Any ideas?

colemabcolemab

I did some testing and I can set the lead email to a dummy address, then add my actual address and send an email but of course it shows the dummy address in the message. 

 

Until salesforce fixes this, I am going to use the no-reply@salesforce.com email address.  Maybe if everyone else uses that address to, the extra load on the email server will cause them to notice and fix the problem.

 

Anyone else have any ideas?