Using render.mergeEmail() to Merge Email Templates with Records in NetSuite

In NetSuite, the render.mergeEmail() function is a powerful tool that allows users to merge an existing email template with various record details. This enables dynamic email generation based on transaction, entity, or other record data. Here’s how it works:

render.mergeEmail({ 
templateId: number, 
entity: RecordRef, 
recipient: RecordRef, 
customRecord: RecordRef, 
supportCaseId: number, 
transactionId: number })
  • templateId (Required): The internal ID of the email template that you want to merge with the specified record.
  • entity: A reference to the entity record (e.g., customer, vendor) that the email will be associated with.
  • recipient: The intended recipient of the email. While this can be included here, it’s more commonly defined in the email.send() function.
  • customRecord: A reference to a custom record that will be included in the merge process.
  • supportCaseId: The ID of the support case, if relevant to the email template.
  • transactionId: The internal ID of the transaction record to be merged with the email template.

After executing the merge, the resulting email content—such as the subject and body—can be accessed through the mergeResult.subject and mergeResult.body properties.

Example:

let mergeResult = render.mergeEmail({
    templateId: -102,
    transactionId: invoiceId
});

email.send({
    author: author,
    recipients: recipients,
    subject: mergeResult.subject,
    body: mergeResult.body,
    attachments: [file.load({ id: fileId })]
});


In this example, the email template with ID -102 is merged with a transaction (invoice) identified by invoiceId. The subject and body of the merged email are then used in the email.send() function, and an attachment is included by referencing a file ID.

Leave a comment

Your email address will not be published. Required fields are marked *