Sending an Email Based on an Email Template in SuiteScripts

in , December 5th, 2024
woman using desktop computer

Using existing email templates or system email templates is common, and knowing how to load them into a SuiteScript is a vital skill.

Code Sample

define(["N/email", "N/render"], 
function (email, render) {
    /**
     * Send an email.
     * @param {integer} templateId
     * @param {integer} recipientId
     * @param {integer} authorEmployeeId
     */
    function sendEmail(templateId, recipientId, authorEmployeeId) {
        //use the render module to load in a template by it's id
        let emailTemplate = render.mergeEmail({
            templateId: templateId,
            recipient: {
                type: "customer",
                id: recipientId,
            },
        });
        //manipulate the template as needed here
        //use the email module to send the email
        email.send({
            author: authorEmployeeId || -5,
            recipients: recipientId,
            subject: emailTemplate.subject,
            body: emailTemplate.body,
        });
    }
    return {
        onRequest: service,
    };

Code Sample Using Built-in NetSuite Flow

System email templates can be sent using the built-in NetSuite flow instead of the code like that outlined above. See this code example:

/**
  * Send the default access notification email. 
*/
sendNotificationEmail: function (customerId, contactId) {
  //load the customer and edit the access lines to send 
  //the email to the customer or contact as needed
    let customer = record.load({
        type: "customer",
        id: customerId,
        isDynamic: true,
    });
    if (!!contactId) {
        let lineNumber = customer.findSublistLineWithValue({
            sublistId: "contactroles",
            fieldId: "contact",
            value: contactId,
        });
        customer.selectLine({
            sublistId: "contactroles",
            line: lineNumber,
        });
        customer.setCurrentSublistValue({
            sublistId: "contactroles",
            fieldId: "giveaccess",
            value: true,
        });
        customer.setCurrentSublistValue({
            sublistId: "contactroles",
            fieldId: "sendemail",
            value: true,
        });
        customer.commitLine({
            sublistId: "contactroles",
        });
    } else {
        customer.setValue({
            fieldId: "giveaccess",
            value: true,
        });
        customer.setValue({
            fieldId: "sendemail",
            value: true,
        });
    }
    customer.save({
        enableSourcing: true,
        ignoreMandatoryFields: true,
    });
}

Author: Sam Gagliardi


Got stuck on a step in this article?

We like to update our blogs and articles to make sure they help resolve any troubleshooting difficulties you are having. Sometimes, there is a related feature to enable or a field to fill out that we miss during the instructions. If this article didn't resolve the issue, please use the chat and let us know so that we can update this article!

Oracle NetSuite Alliance Partner & Commerce Partner

If you have questions about how our team can support your business as you implement NetSuite or SuiteCommerce, feel free to contact us anytime. Anchor Group is a certified Oracle NetSuite Alliance Partner and Commerce Partner equipped to handle all kinds of NetSuite and SuiteCommerce projects, large or small!

 
 

Want to keep learning?

Our team of NetSuite and ecommerce professionals has written articles on a wide variety of topics, from step-by-step tutorials, to solution recommendations, available support services, and more!

Your cart