1

Is there a way for me to know, for a specific EmailTemplate.Id, which objects are used to merge fields?

E.g:

Template:

My template use {! Contact.Name } but also {! Invoice.Id }

Soql query:

[SELECT Body, ?? FROM EmailTemplate WHERE Id = 'xxx']; // To get ['Contact, Invoice']

And also giving to the template multiple targetObjectId (here the Id of a Contact and the Id of a Invoice).

mail.setTargetObjectIds(contactId, invoiceId);
2

There is a conceptual issue here

When you use setWhatId(..) you are setting the center of attention object to use in the email template. If a Visualforce email template, then this object is bound to the template's relatedTo= attribute.

The target object then is used by Salesforce to instantiate a standard controller for that object and because of your markup, implicitly binds parent and children objects as well.

So, If the whatId object is Invoice, and Invoice has lookups to Contact (like a bill to contact), your email template would use references like this:

relatedTo.BilltoContact__r.FirstName

When you use setTargetObjectId(..) you are identifying the recipient of the email. This is bound to the recipientType attribute in the VF template.

So, your use case might then be

emailObj.setTargetObjectId(someContactId);  // bound to recipientType
emailObj.setWhatId(someInvoiceId);  // bound to relatedTo
6
  • Except there's Recipient Type in addition to Related To Type. OP seems to want to be able to query these back after the fact. – Adrian Larson Dec 29 '16 at 15:17
  • @AdrianLarson .. sheesh, got this backwards; edited – cropredy Dec 29 '16 at 15:29
  • And what if the template is using more than Contact and Invoice objects as fields provider (E.g. : the Property__c object) that is linked to an Invoice with multiple relations in between ? I really still do not get how to inject them correctly in Apex or maybe if I write the relations inside the template E.g. : My template use {! Invoice.Customer__c.Name } but also {! Invoice.Id } – Simonp Dec 30 '16 at 7:13
  • {!Invoice__c.Order_Responsible__r.Email__c} this does not work at rendering – Simonp Dec 30 '16 at 7:23
  • @SimonPlacentino - If using a VF template, the expression would be {!relatedTo.Order_Responsible__r.Email__c} where relatedTo="Invoice__c" and setWhatId(someInvoiceId) – cropredy Dec 30 '16 at 17:07
0

My understanding is that you are trying to query for the Recipient Type and Related To Type you set on the Email Template when you create it.

Email Template Creation Page

This data is not exposed via the API. If you look at the SOAP API Developer Guide reference on EmailTemplate, the only field that looks promising is EntityType, but it is only defined for certain UIType values, and in my org it doesn't seem to be any help in your quest (our templates are all designed for Salesforce Classic). Which is unfortunate, because this looks designed to hold the Related To Type:

When UIType is 2 (Lightning Experience) or 3 (Lightning ExperienceSample), EntityType indicates which entities this template can be used with (for example, an account, lead, and so on). Valid values are standard object ID prefixes: 001 for account, 003 for contact, 006 for opportunity, and 00Q for lead, 500 for case, and 701 for campaign.

There doesn't even seem to be any way to tell via the UI which objects can be used. With a Visualforce Email Template, you can at least see them as recipientType and relatedToType attributes on your <messaging:emailTemplate> tag. For HTML and Plain Text emails, I think you're stuck looking at the merge fields.

1
  • Hey. I'm not using Visualforce page but Lightning Component and Apex controller. I want to get formatted email to be rendered in my component on the client side. Anw I still do not get how to even w/ your good explainations.. – Simonp Dec 30 '16 at 7:09

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.