SuiteScript 2.0: Sample code for sending an email using a scriptable template


I was trying to come up with a SuiteScript 2.0 code that sends an email using a scriptable template. With SuiteScript 1.0, you can achieve this by using the nlapiCreateEmailMerger() API. SuiteAnswers does not have an exact SuiteScript 2.0 sample for it but I did saw in the SuiteScript 1.0 to SuiteScript 2.0 API Map document its SuiteScript 1.0 equivalent is the render.mergeEmail method.

After some testing, here’s the working snippet that I came up with. Hope you find it useful!

Important Notes:

  • The render.mergeEmail options like transactionId does not attach the message record to that record (the sales order record, for example). It is only used to render the record’s values based on the variables indicated on the scriptable template.
  • To successfully attach the email to the desired record, use the relatedRecords parameter from the email.send method.
  • The templateId variable is the internal ID of the email templates as seen on Documents > Templates > Email Templates.
var transactionId = 4022538; 
var mergeResult = render.mergeEmail({
templateId: 25,
entity: null,
recipient: null,
supportCaseId: null, 
transactionId: transactionId,
customRecord: null
});
var emailSubject = mergeResult.subject; 
var emailBody = mergeResult.body; 
email.send({
author : author, 
recipients : emailRecipients, 
subject : 'test', 
body : emailBody, 
relatedRecords : {
transactionId : transactionId
}
});

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s