Merge a Template via nlapiCreateEmailMerger() and Send It Via nlapiSendEmail()

Description 
This article discusses how users may utilize the nlapiCreateEmailMerger() feature to merge templates and send it as emails.
Solution
First the user needs to create an email template via UI. That can be done by navigating to Documents > Templates > Email Templates > New.

Select Text Editor under the template tab.

Select Field Type based on the record type under the ‘Field Type’ list.

Select the fields that you want to use and populate for the template.

For populating the variables/fields that were set on your template, please refer to the code snippet provided below:

SuiteScript 1.0:

var recordid = nlapiGetRecordId(); // Load Record ID        
        var emailMerger = nlapiCreateEmailMerger('2'); // Initiate Email Merger
        emailMerger.setTransaction(recordid); // Set the ID of the transaction where you are going to fetch the values to populate the variables on the template
        var mergeResult = emailMerger.merge(); // Merge the template with the email

        var emailSubject = mergeResult.getSubject(); // Get the subject for the email
        var emailBody = mergeResult.getBody(); // Get the body for the email

        nlapiSendEmail(nlapiGetUser(), customerid, emailSubject, emailBody, null, null, null, filesample); // Send the email with merged template

Leave a comment

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