Send an Email using a Template with SuiteScript
Created: February 2, 2024
This is a sample chapter adapted from the "Sending Email with SuiteScript 2.1" Cookbook, part of the SuiteScript by Example series.
Most of the time when we're sending out business email from NetSuite, we want a consistent look and feel along with branding and other official information. We typically use company Email Templates to accomplish this, and thankfully we can leverage these same Templates in SuiteScript.
The
N/render
module
enables us to work with Templates. We import the module and use its
mergeEmail()
method
to generate the subject and body of our email.
This code example uses the
require
function for importing modules. This allows you to copy and paste the snippets directly into the debugger or your browser's developer console and run them. For more on how to test SuiteScript in your browser's console, watch my tutorial video .
/**
* Sends an email using an Email Template
*
* @author Eric T Grubaugh <eric@stoic.software> (https://stoic.software/)
*/
require(['N/email', 'N/render'], (email, render) => {
const template = render.mergeEmail({
templateId: 17,
entity: {
type: 'employee',
id: 19
}
})
email.send({
author: -5,
recipients: [19],
subject: template.subject,
body: template.body
})
})
Usable Email Templates are listed under
Documents > Templates > Email Templates
.
Rendering Email Templates
render.mergeEmail()
merges specific record data into an existing Email
Template to generate the subject
and body
of an email.
Template 17
used above has this snippet in it:
<p><b>It's Playoffs Time!</b></p>
<p><b>NetSuite invites ${entity.entityId}</b></p>
mergeEmail()
substitutes data from Employee 19
into the email body like
this:
Note you must use the numeric internal ID of the Template; the more friendly
text ID that begins with custemailtmpl_
is unfortunately not supported.
mergeEmail()
also supports merging in data from a Support
Case (supportCaseId
), a Transaction (transactionId
), a Custom
Record (customRecord
), and a Recipient (recipient
). See the
render.mergeEmail()
documentation
for more details.