Salesforce Email Template Variables: Insert Dynamic Data into Every Template Type

This guide maps every variable type—SML, HML, Visualforce globals, child record iteration, and conditional rendering—so you can match the right approach to your personalization requirements.

Template variables are data placeholders that transform static Salesforce emails into personalized communications—pulling recipient names, record details, and computed values into your message at send time. Salesforce implements variables differently across template types: Classic uses SML ({!Object.Field}), Lightning uses HML ({{{Category.Field}}}), and Visualforce exposes powerful global variables like {!relatedTo} and {!recipient} with cross-object traversal. This guide maps every variable type across all Salesforce email templates so you can choose the right approach for your personalization needs.

SML Variables in Classic Templates: The {!Object.Field} System

Classic email templates use Salesforce Merge Language (SML) variables enclosed in {! } delimiters following the pattern {!Object.FieldAPIName}. The template editor provides a variable picker organized by field type—Contact Fields, Lead Fields, User Fields, Organization Fields—so admins insert variables without memorizing API names. SML variables resolve at send time and are the only type supported in workflow email alerts, Process Builder, and Flow Builder email actions.

SML supports standard fields, custom fields, formula fields, and picklists on the template’s primary object. User variables ({!User.FirstName}) insert sender data; Organization variables ({!Organization.Name}) pull company-wide information. However, SML cannot traverse relationships—{!Contact.Account.Name} does not resolve, as confirmed in Salesforce’s merge field considerations documentation. See our template creation guide for step-by-step instructions.

HML Variables in Lightning Templates: Categories and Triple-Curly Syntax

Lightning templates use Handlebars Merge Language (HML) variables: {{{Recipient.FirstName}}}, {{{Sender.Email}}}, {{{Organization.Name}}}. HML organizes variables into four categories—Recipient, Sender, Organization, and Related Entity Type. The Lightning Email Template Builder provides a visual variable picker in every Rich Text component—select a category, choose the field, and click Insert.

HML offers cleaner syntax than SML but shares the same cross-object limitation. Salesforce’s Handlebars Merge Language reference documents supported categories and syntax. Critically, Lightning templates with HML variables cannot be used in workflow rules or Process Builder, limiting email automation use cases. Lightning also lacks Send Test and Verify Merge Fields, making variable validation more manual.

Visualforce Template Variables: relatedTo, recipient, and apex:repeat

Visualforce templates unlock the most powerful variable system through two globals—{!relatedTo} and {!recipient}—combined with Visualforce markup. The {!relatedTo} variable references the Related To Type object, providing access to all fields and child relationships. The {!recipient} variable references the Recipient Type (Contact, Lead, or User). Together, they enable cross-object data access that SML and HML cannot achieve. Salesforce’s Visualforce email templates developer guide details the full variable framework.

The apex:repeat tag iterates child records, displaying tables of related data inside emails—for example, looping through every Case on an Account. Custom controller variables extend this further: an Apex controller can execute SOQL, perform calculations, and return computed values as template variables. For cross-object variables without Visualforce complexity, MassMailer supports multi-level merge field traversal through its visual builder—no code required.

Conditional Variables and Dynamic Content Rendering

SML and HML variables are strictly value placeholders—they insert data but cannot control what content appears. Conditional rendering requires Visualforce: the rendered attribute accepts Boolean expressions like rendered="{!relatedTo.Amount > 50000}" to show content selectively. You can chain conditions with AND, OR, and NOT operators, and use ISBLANK functions to handle empty variables gracefully.

For conditional content without Visualforce development, MassMailer’s email template builder provides dynamic content blocks that render different content based on recipient attributes—all within a drag-and-drop interface, eliminating the need for Apex or markup code.

Variable Validation and Common Pitfalls

Common pitfalls include: variables rendering blank because the record’s field is empty (SML and HML show nothing with no warning), silent failures from field-level security restrictions on the running user, syntax errors from missing delimiters or misspelled API names, and Lightning sanitization stripping variables embedded in custom HTML blocks.

Validation is non-negotiable. In Classic, use Send Test and Verify Merge Fields with a representative record. In Lightning, preview with a live record from the builder. For Visualforce, send test emails and check both HTML and plain text versions. Build a variable testing checklist as part of your email deliverability workflow.

Extending Template Variables with Native Salesforce Tools

When native variable systems hit their limits, organizations face a choice between Visualforce development and external platforms. MassMailer provides a third option as a 100% native Salesforce app—its builder supports variables from any object with relationship traversal up to six levels deep, child record loops (similar to apex:repeat but without code), and conditional content sections.

Combined with email tracking that logs every engagement as a native Salesforce record and the ability to bypass the daily email limit, MassMailer delivers enterprise-level variable capabilities without enterprise-level complexity.

Your email variables should pull data from any object and render conditionally—without writing Apex or Visualforce. MassMailer makes that possible with a visual builder and multi-level variables. Schedule a live demo and see template variables in action.

Key Takeaways

  • Classic SML variables ({!Object.Field}) work in all automation tools but cannot traverse object relationships.
  • Lightning HML variables ({{{Category.Field}}}) offer cleaner syntax but share the same cross-object limitation.
  • Visualforce provides the most powerful variables via {!relatedTo} and {!recipient} with apex:repeat for child records.
  • Conditional rendering requires Visualforce or Apex—SML and HTML cannot show/hide content based on data.
  • Always validate variables before deployment using Send Test (Classic), live preview (Lightning), or test emails (Visualforce).
  • MassMailer extends variable capabilities with multi-level traversal, child record loops, and conditional blocks—no code needed.