You need to sign in to do that
Don't have an account?
Alex 2.0
Send Email to non User/Contact
Hi,
I'm building a VisualForce page that needs to send a report to an email address entered in a textfield (not a User or a Contact) using a VisualForce template
According to the docs, targetObjectId is an optional field when sending an email, but I keep getting this error:
SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing targetObjectId with template
If I set the TargetObjectId to an existing contact, it all works fine, but I do not want these email to always go to a contact.
Is there anything I'm missing?
Thanks so much for your help!
--Alex
Here's the code:
Messaging.SingleEmailMessage emailMsg = new Messaging.SingleEmailMessage();
emailMsg.setSaveAsActivity(false);
emailMsg.setWhatId(AccountProfile.Id);
emailMsg.setTemplateId([select Id from EmailTemplate where DeveloperName = 'AccountProfileReportEmail'].Id);
String[] emailAddresses = emailTo.split(';');
emailMsg.setToAddresses(emailAddresses);
Messaging.SendEmailResult[] sers;
//Insert PDF as quote attachment & send email
try
{
//insert profileAttachment;
sers = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {emailMsg});
}
catch(DmlException dmle)
{
for (Integer i=0; i < dmle.getNumDml(); i++)
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, dmle.getDmlMessage(i)));
Database.rollback(sp);
return null;
}
catch(Exception e)
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
Database.rollback(sp);
return null;
}
Correct
If you want to use an email template you need to specify a setTargetObjectId which must reference a contact, lead, or user.
All Answers
I see you are using "setToAddresses" and an email template.
The doc also says that "setToAddresses" is allowed only when an email template is NOT used.
Thanks devNut!
but how do I set the To Address for the email then?
--Alex
Does it mean that I can't send an email to an email address specified in a text field (not a user, lead, or contact) when using a template?
--Alex
Correct
If you want to use an email template you need to specify a setTargetObjectId which must reference a contact, lead, or user.
Thanks so much.
Would be nice if that wasn't the case. Maybe in a future release...
-Alex
No problem.
You can always post an idea on http://ideas.salesforce.com/