3

I have a custom object that have a custom email field. I need to send email to the email address which is stored in that email field.

But as I read we cant use setTargetObjectID with custom objects. Is there any workaround this? I have tried creating a lookup relation to contact and then set the contact id and I have set the setToAddresses method of Messaging.SingleEmailMessage to the emailid field from my custom object.

But I get the error: Messaging.SendEmailError[getMessage=The target object's email address "null" is not valid;getStatusCode=INVALID_EMAIL_ADDRESS;

Want to know what values needs to be set here and how can I use setTargetObjectID with a custom object without creating a lookup of the custom field with contact field.

This is the piece of code:

    private Messaging.SingleEmailMessage createEmail(EmailTemplate template, 
    Patient__c patient)
    {
    try{

        //System.debug('email id is: ' + patient.Email_Id__c);
        if(patient.Contact__r.id != null) {

            List<String> toaddress = new List<String>();
            toaddress.add(patient.Email_Id__c);

            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setTemplateId(template.id);
            //System.debug('temalate id: ' + template.id);
            email.setTargetObjectId(patient.Contact__r.id);
            email.setWhatId(patient.id);
            email.setorgWideEmailAddressId(owe.get(0).id);
            email.setToAddresses(toaddress);
            return email;
        }
        return null;
    }

Appreciate any kind of help here. Thanks a lot.

2 Answers 2

6

To avoid the error, use setTreatTargetObjectAsRecipient to false:

email.setTreatTargetObjectAsRecipient(false);
3
  • do I need to add this line and then try? I did that but this gives me a different error: Messaging.SendEmailError[getMessage=Organization-Wide Email Address has not be verified for use.;getStatusCode=UNVERIFIED_SENDER_ADDRESS;
    – user905
    Commented Sep 1, 2017 at 21:15
  • @9codie05 That's a different error. You need to verify your org-wide email address, or comment it out and send it as yourself.
    – sfdcfox
    Commented Sep 1, 2017 at 21:17
  • I did verify the email but then I was getting error with whatId so I used a different approach and was able to solve it. Thanks @sfdcfox for your help :)
    – user905
    Commented Sep 4, 2017 at 4:52
1

So as it was given we cant use the custom object id as the setTargetObjectId. So I used another similar approach. First you will have to pick any random contact and using that contact set your email template and form the email message. Set the save point. Send it. Roll it back. That saves the state of the email.

The key here is to understand that Salesforce doesn’t send an email immediately when the sendEmail method is executed. Instead, Salesforce waits until the very end of the transaction. If you roll back the transaction, Salesforce doesn’t send the email at all.

Link: https://opfocus.com/sending-emails-in-salesforce-to-non-contacts-using-apex/.

Was able to solve my issue by using the approach in this link. Quite helpful.

6
  • 2
    You do know that using the rollback method is no longer necessary, right? You can just use Messaging.renderEmailTemplate instead.
    – sfdcfox
    Commented Sep 4, 2017 at 4:57
  • @sfdcfox Messaging.renderEmailTemplate gets counted toward query limits, could you please clarify why shouldn't one use the way mentioned in this answer? Commented Apr 16, 2020 at 8:10
  • @AdityaVijay The rollback method counts against DML limits and the very limited Messaging.sendEmail limits. The renderEmailTemplate method was designed to be less of a resource burden than the rollback method. I'm not saying it should necessarily be avoided, just that the newer method is simpler to use.
    – sfdcfox
    Commented Apr 16, 2020 at 12:41
  • @sfdcfox thanks for your explanation, I just added targetObjectId as a user, and wrote email.setTreatTargetObjectAsRecipient=false, and added ToAddresses, this is how I did it... do you see any problem in this? Commented Apr 16, 2020 at 12:55
  • @AdityaVijay Aside from the 5,000/day email limit you'll need to keep in mind, there's nothing wrong with the technique.
    – sfdcfox
    Commented Apr 16, 2020 at 12:57

You must log in to answer this question.

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