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 liketransactionId
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 theemail.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 } });