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)

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

            List<String> toaddress = new List<String>();

            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            //System.debug('temalate id: ' + template.id);
            return email;
        return null;

Appreciate any kind of help here. Thanks a lot.


To avoid the error, use setTreatTargetObjectAsRecipient to false:

  • 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 Sep 1 '17 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 Sep 1 '17 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 Sep 4 '17 at 4:52

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.

  • 1
    You do know that using the rollback method is no longer necessary, right? You can just use Messaging.renderEmailTemplate instead. – sfdcfox Sep 4 '17 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.