How to Use Handlebars in a SuiteScript Sent Email Template

in , , November 12th, 2024
a person typing on a keyboard at a desk

We ran into a situation where a client wanted to send an email from a SuiteScript and also wanted it to use an email template for ease of use and styling purposes. However, they needed some of the values in the email to be defined or calculated by the script at the time of sending.

Using handlebars is a good way of setting a whole bunch of values relatively easily. Here is a step-by-step walkthrough of how we achieved this for our client! Follow along with this article to replicate a similar use of handlebars for your use case.

Note: The steps below don’t necessarily need to be done in this order.

Step 1: Upload the Handlebars File into the File Cabinet

You can download it from here:

Then, upload it to the file cabinet and put it in a logical folder location, not just floating helplessly in the great void of the SuiteScripts folder, like an unmoored specter upon the waves of a metaphysical ocean of existential questioning.

Step 2: Define it in Your Script

You’ll want to change the file location in your script definition based on where you actually place and name the handlebars file in the file cabinet. See the code example below:

define([
    "N/file",
    "N/record",
    "N/redirect",
    "N/task",
    "/SuiteScripts/Anchor Group/common/lib/handlebars.min.js", //Here
    "/SuiteScripts/Anchor Group/common/AG_CM_Logger.js",
],
(
    N_file,
    N_record,
    N_redirect,
    N_task,
    Handlebars, //Here
    Logger,
) => {

Step 3: Create the Email Template in NetSuite

Next, you will need to create the email template in NetSuite using handlebars variables/placeholders.

The format here is “”

You can use handlebars logic too, as shown in the screenshot in the form of: {{#if variableThatMightBeTrue}} Some words here {{/if}

Step 4: Put it all Together in the Script

 function sendPotentialConflictEmail(emailTemplateId, objectHoldingVariableValuesData={}) {
        //First load the template record.
        const emailTemplate = N_record.load({
            type: N_record.Type.EMAIL_TEMPLATE,
            id: emailTemplateId
        });
        
        //Get the html string from the template record.
        const emailBody = emailTemplate.getValue('content');
        
        //Get the the Handlebars Object (which will act something like a function here) by referencing the Handlebars file from step 1, and passing the template html string
        const template = Handlebars.compile(emailBody);
        
        //Pass the data object holding the values that we want in the template
        const formattedEmailBody = template(objectHoldingVariableValuesData);


        //in this example we aren't actually sending the email right away. We are passing this filled-out template to a Map/Reduce script which will send it out.
        //However, the email template should be good by this point, filled with all that values we wanted.
        const notificationTask = N_task.create({
            taskType: N_task.TaskType.MAP_REDUCE
        });


        notificationTask.scriptId = "customscript_ag_mr_ca_massemail";
        notificationTask.params = {
            "custscript_ca_me_email_info": {
                subject: emailTemplate.getValue('subject'),
                body: formattedEmailBody
            },
        };


        const notificationTaskId = notificationTask.submit();
    }

Author: JP Terneus


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 professionals has written articles on a wide variety of NetSuite topics, from SuiteCommerce tips, to recommended NetSuite solutions, to available support services, and more! 

Your cart