My case is i want to send email with attachment in custom object using single email message using HTML Email Template (with letterhead).

Current condition i successed to make the email same like email template (HTML body) by using these methods:

email.setTargetObjectId('0055D000001DHyJ'); //user id

But the problem is, fields in email template become blank. For Example:

Email template

Dear {!Invoice__c.Customer},


Dear ,

I also try use .replace but it make email body become blank.

Is there any solution or workaround for this case?

Refference : http://hareeshgoudsalesforce.blogspot.com/2016/06/stop-sending-email-to-settargetobjectid.html

  • Is the setTargetObjectId being set to a User or Contact or Lead ?
    – Robs
    Jul 11, 2018 at 11:18
  • Hi, its working now. I post the code at 2nd answer below. So for custom object you should put setTargetObjectId as ContactId
    – Enriko
    Jul 11, 2018 at 12:13

2 Answers 2


I am not sure exactly what your goal is and the blog post is very confusing to me.

What I understand is: {!Invoice__c.Customer} needs to be filled in the result.

For this case, the WhatId should be of Type Invoice__c and you should be using the appropriate template methods:

email.setToAddresses(emailResults); email.setTargetObjectId('0a3xxxxxx'); //<= user / recipient id email.setWhatId('0a3xxxxxx'); // <= Invoice Id email.setTreatTargetObjectAsRecipient(false); email.setTemplateId(tem.Id);


If you specify a contact for the targetObjectId field, you can specify an optional whatId as well. This helps to further ensure that merge fields in the template contain the correct data.

  • Hi, thanks for your respond. I try to replace using Invoice ID in setTargetObjectId but it give error message "SendEmail failed. First exception on row 0; first error: INVALID_TYPE_FOR_OPERATION, Only User, Contact, Lead, or Person objects are allowed for targetObjectId: a045D000009HDVm.: [targetObjectId, a045D000009HDVmQAO]"
    – Enriko
    Jul 11, 2018 at 11:05
  • My goal is want to fill the fields in HTML Email template using single email message, .setReplace() not working for this case :(
    – Enriko
    Jul 11, 2018 at 11:07
  • sorry, i mixed something up. see my edited answer. Jul 11, 2018 at 11:16
  • Now i get this error 'SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, WhatId is not available for sending emails to UserIds.: [whatId, a045D000009HDVm]'. FYI i write email.setWhatId(d.id)
    – Enriko
    Jul 11, 2018 at 11:25
  • 1
    Updated: I input TargetObjectId as ContactId and input WhatId as CustomObjectId. And its work bro, thanks for your help. Appreciate it!
    – Enriko
    Jul 11, 2018 at 11:46
 email.setTargetObjectId('0035D00000Rc4bU'); //contact id (you can do soql)
 email.setWhatId(d.id); // custom object id

Updated : Final solution

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.