I would like to send an email with a template with data that comes from a custom object. I saw that with the setTargetObjectId one must use contact leads or user but me instead I used a custom object so impossible to send the mail. If you have a solution to overcome this situation
1 Answer
Use setWhatId
to specify the custom object to use as the base merge object. Use setTargetObjectId
to specify the User or Contact to send the email to. You can use both on the same Messaging.SingleEmailMessage
object.
-
Thank you for your interest I already know this, the problem is that the contacts are not in the contact object but in a custom object Commented Sep 23, 2019 at 16:33
-
@KhadimRassoulNDEYE You would still use
setWhatId
to specify the target for merge fields, but you'll have to send the email by email address (setToAddresses
).– sfdcfox ♦Commented Sep 23, 2019 at 18:27 -
@KhadimRassoulNDEYE Alternatively, if you're trying to say that you want to use the custom object as a "contact"--you can't. Only User, Lead, and Contact records may be set by
setTargetObjectId
. This is why Salesforce advises that you don't try to build custom "contact" objects, because of limitations like this one.– sfdcfox ♦Commented Sep 23, 2019 at 18:29 -
that's the problem, the admins used a custom contact object, thank you that even Commented Sep 24, 2019 at 12:24