๐ Creating Dynamic Email Templates in NetSuite (FreeMarker + SuiteScript)
Introduction
NetSuite provides a flexible way to design and automate custom email templates โ perfect for invoices, order confirmations, customer communications, and workflow notifications.
Using FreeMarker syntax, you can dynamically insert record data into templates. Combine that with SuiteScript or SuiteFlow, and you can send personalized, branded emails that automatically trigger during business processes.
This tutorial walks you through:
- Creating email templates using FreeMarker
- Adding dynamic record fields and logic
- Sending templates via SuiteScript or workflows
- Pro tips for styling and testing
๐ก Why Use Custom Email Templates?
Goal | Example |
---|---|
Personalized Emails | Include customer name, order total, and expected ship date |
Automated Notifications | Trigger emails after fulfillment or approval |
Professional Branding | Add logos, HTML styling, and signatures |
Multi-language Messaging | Show content dynamically based on customer locale |
Marketing or Engagement | Send reactivation or renewal reminders |
๐งฑ Step-by-Step: Creating a Custom Email Template
Step 1: Navigate to Template Setup
Go to:
Documents โ Templates โ Email Templates โ New
Step 2: Add Template Information
Field | Description |
---|---|
Name | Unique template name (e.g., Order Confirmation Template) |
Record Type | Select record the template will be tied to (e.g., Sales Order) |
Available For | Email, Workflow, or Script |
Subject | Use FreeMarker variables (e.g., Order #${transaction.tranid} ) |
Step 3: Add Body Content with FreeMarker
In the body editor, you can use FreeMarker syntax to pull data dynamically:
<p>Dear ${customer.entityid},</p>
<p>Thank you for your order <strong>#${transaction.tranid}</strong> placed on
${transaction.trandate?string("MMMM dd, yyyy")}.</p>
<p>Your order total is <strong>${transaction.total}</strong>.</p>
<p>You can view your order here:
<a href="https://{{companyURL}}/app/accounting/transactions/salesord.nl?id=${transaction.id}">
View Order Details</a></p>
<p>Best regards,<br/>
${companyInformation.companyname} Team</p>
โ
Tip: Use ${record.fieldid}
or ${transaction.fieldid}
for record-level values.
Step 4: Add Company Branding (Optional)
Use inline CSS for styling:
<div style="font-family:Arial; font-size:14px;">
<h2 style="color:#0066cc;">Order Confirmation</h2>
<p>Thank you for choosing ${companyInformation.companyname}!</p>
</div>
You can also insert your logo:
<img src="https://yourdomain.com/images/logo.png" width="200"/>
Step 5: Test the Template
- Click Preview to test data placeholders.
- Select a sample record (e.g., a Sales Order).
- Verify all variables render correctly (not blank or missing).
๐ง FreeMarker Quick Reference
FreeMarker Function | Example | Description |
---|---|---|
${field} | ${transaction.tranid} | Displays field value |
<#if> | <#if transaction.total > 500> | Conditional logic |
<#list> | <#list transaction.item as line> | Loop through lines |
<#assign> | <#assign totalLines = transaction.item?size> | Create variable |
<#include> | <#include "footer.ftl"> | Include another template |
Example: Add Conditional Section
<#if transaction.status == "Pending Approval">
<p style="color:red;">Your order is pending approval.</p>
<#else>
<p>Your order is being processed.</p>
</#if>
Example: Loop Through Line Items
<table border="1" cellpadding="6">
<tr><th>Item</th><th>Qty</th><th>Rate</th><th>Amount</th></tr>
<#list transaction.item as line>
<tr>
<td>${line.item@label}</td>
<td>${line.quantity}</td>
<td>${line.rate}</td>
<td>${line.amount}</td>
</tr>
</#list>
</table>
โ๏ธ Sending Emails via SuiteScript
Once your template is created, send it from any script (User Event, Workflow, or Suitelet):
/**
* @NApiVersion 2.1
*/
define(['N/email', 'N/record', 'N/runtime'], (email, record, runtime) => {
const sendOrderEmail = (salesOrderId) => {
email.send({
author: runtime.getCurrentUser().id,
recipients: 'customer@example.com',
subject: 'Your Order Confirmation',
body: email.merge({
templateId: 123, // ID of your email template
entity: { type: 'customer', id: 456 },
transactionId: salesOrderId
}).body
});
};
return { sendOrderEmail };
});
โ
Result:
The system merges your FreeMarker email template with the Sales Order data and sends a professional, dynamic email.
๐ก Integrating Email Templates in Workflows
- Create a Workflow on a record (e.g., Sales Order).
- Add a State โ Action โ Send Email.
- Choose your custom template.
- Set condition (e.g., Status = โBilledโ).
- Schedule or trigger on field change.
Result:
Automatic email notifications with dynamic data and branding.
๐งฉ Real-World Use Cases
Scenario | Description |
---|---|
Order Confirmation | Triggered when Sales Order is approved |
Invoice Reminder | Sent when invoice due date approaches |
Shipment Notification | Sent when Item Fulfillment is completed |
Customer Welcome | Workflow email when new Customer is created |
Payment Receipt | Auto-send when Payment record is saved |
โก Styling & Best Practices
- Use inline CSS (external stylesheets are not supported).
- Avoid images over 200 KB to improve deliverability.
- Use plain text fallback for critical notifications.
- Add tracking parameters (UTM) for marketing emails.
- Test across email clients (Gmail, Outlook, Apple Mail).
- Use
Preview Template
for validation before live use.
๐งฎ Common Issues & Fixes
Problem | Cause | Solution |
---|---|---|
Variables not showing | Wrong record context | Use correct field path (e.g., transaction.total ) |
HTML broken | Unclosed tags | Use online validator before saving |
Template not selectable in script | Missing โAvailable For: Scriptโ option | Enable before saving |
No preview data | No sample record selected | Choose record when previewing |
๐ Related Tutorials
- ๐ Custom Buttons & Actions in NetSuite
- ๐ Advanced PDF Templates with FreeMarker
- ๐ Automating Approvals with SuiteFlow
โ FAQ
Q1. Can I attach files or PDFs to emails sent via templates?
Yes โ in SuiteScript, use the attachments
parameter in email.send()
to attach PDFs or files.
Q2. Can I include custom fields in templates?
Yes, use ${transaction.custbody_fieldid}
or ${customer.custentity_fieldid}
.
Q3. Can I send different templates based on role or region?
Yes โ create multiple templates and use conditions in workflows or script logic.
Q4. Is there a size limit for email templates?
Yes, up to ~50 KB HTML content is recommended for optimal rendering.
๐งญ Summary
Custom Email Templates in NetSuite make communication personalized, automated, and professional.
By combining FreeMarker logic and SuiteScript, you can deliver dynamic, data-driven messages that match your business workflow โ without manual intervention.
Mastering this feature not only streamlines communication but also builds stronger customer relationships.
Leave a Reply