0

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

2
  • Are you using standard email or enhanced email (with EmailMessage object)? Are you in Lightning or Classic?
    – crmprogdev
    Commented Sep 23, 2019 at 12:14
  • Im in Lightning and i use standard email Commented Sep 23, 2019 at 16:47

1 Answer 1

1

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.

4
  • 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .