Newer Version Available

This content describes an older version of this product. View Latest

renderEmailTemplate()

Replaces merge fields in text bodies of email templates with values from Salesforce records, even for polymorphic fields. The email template bodies and their corresponding whoId and whatId values are specified in the argument.

Syntax

Usage

The renderEmailTemplate() call is equivalent to rendering merge fields when sending an email with a custom template through the sendEmail() call.

The renderEmailTemplate() call can take up to 10 RenderEmailTemplateRequest elements in its array argument, and each RenderEmailTemplateRequest can contain up to 10 template bodies. Each request is independent from the other requests in the array—an error in one request doesn’t affect the other requests. Similarly, an error in one template body doesn’t cause an error in other text bodies within the same request.

The renderEmailTemplate() call substitutes a merge field with the value of either the whatId or whoId in RenderEmailTemplateRequest:

  • If the merge field references a non-human object, it’s replaced with the corresponding value of whatId. For example, if a merge field references an account or opportunity, the whatId value is substituted.
  • If the merge field references a human object, it’s replaced with the corresponding value of whoId. For example, if a merge field references a contact, lead, or user, the whoId value is substituted.
The whatId and whoId field values of RenderEmailTemplateRequest are validated for each request. If the whatId doesn’t reference a valid what ID (a non-human object), or the whoId doesn’t reference a valid who ID (a human object), an error is set for the request.

Sample Code—Java

In this sample, the renderEmailTemplate() call substitutes all contact merge fields with the value from the specified whoId argument. Similarly, the call substitutes the opportunity merge field ({!Opportunity.Name}) with the specified whatId value. The second template body in this sample has an incorrect merge field ({!Contact.SNARF}), which causes an error on the second template. However, the entire template rendering request is successful.

Let’s say you run this sample by specifying a valid contact ID for the first argument (whoId) and null for the second argument (whatId). The second template has one error set, for the incorrect merge field. The response looks like the following.

RenderEmailTemplateRequest

Name Type Description
templateBodies string[] An array of text bodies that can contain merge fields, such as {!Account.Phone} or {!Contact.Name}.
whatId reference References a non-human object, such as an account, an opportunity, a campaign, a case, or a custom object. The whatId is polymorphic, which means that it’s an ID that can refer to more than one type of object, such as a case or an opportunity.
whoId reference References a human object, such as a lead, contact, or user. The whoId is polymorphic, which means that it’s an ID that can refer to more than one type of object.