Sample SuiteScript 2.0 Application Code for Email
This guide covers the structure and coding of a SuiteScript 2.0 email application, emphasizing best practices in .ss and .ssp files.
TL;DR
This article discusses the structure and coding of a SuiteScript 2.0 application designed for sending emails and provides a sample implementation. Understanding this will help developers leverage SuiteScript effectively for email applications.
What Is SuiteScript 2.0?
SuiteScript 2.0 is NetSuite's JavaScript-based API that allows developers to create and customize business applications. It enables scriptable interactions with NetSuite records and business logic.
How to Structure SSP Application Files
All SuiteScript 2.0 SSP application files must begin with the following directive:
<%@ NApiVersion="2.x" %>This directive ensures the script utilizes the latest minor version of SuiteScript 2.0. If this directive isn't the first line in your .ssp file, it will be incorrectly treated as a SuiteScript 1.0 file, resulting in an error.
Important Note
You cannot utilize the @NScriptType annotation in either .ss or .ssp files.
Sample Application Structure
Here's a simple example structure of an email application comprising three primary files:
- Entry Point:
[Entry Point: emailSender.ssp] - Service:
[Service: emailService.ss] - Custom Module:
[Custom Module: emailHandler.js]
Sample Code for Entry Point - emailSender.ssp
This code represents an entry point form for sending emails:
1<%@ NApiVersion="2.x" %>2<html>3<head>4<!--add css, js packages here -->5<style>6 input::placeholder, textarea::placeholder {7 font-size: 12px;8 }9 10 .formfield * {11 vertical-align: middle;12 }13</style>14</head>15<body>16<div>17 <h2>Email Sender</h2>18 <form action="/" method="POST" id="email-form">19 <fieldset>20 <legend>Mandatory fields</legend>21 <p>22 <label for="recipients"><b>Email</b></label>23 <input type="email" placeholder="recipient1@example.com, recipient2@example.com,..." name="recipients" size="50" required>24 </p>25 <p>26 <label for="subject"><b>Subject</b></label>27 <input type="text" placeholder="Subject..." name="subject" size="50" required>28 </p>29 <p class="formfield">30 <label for="body"><b>Body</b></label>31 <textarea placeholder="Email body..." name="body" rows="10" cols="70"></textarea>32 </p>33 </fieldset>34 <fieldset>35 <legend>Optional fields</legend>36 <p>37 <label for="cc"><b>Cc</b></label>38 <input type="cc" placeholder="cc1@example.com, cc2@example.com,..." name="cc" size="50">39 </p>40 <p>41 <label for="bcc"><b>Bcc</b></label>42 <input type="bcc" placeholder="bcc1@example.com, bcc2@example.com,..." name="bcc" size="50">43 </p>44 </fieldset>45 <p>46 <input type="submit" id="send-email-button" value="Send Email">47 <input type="submit" id="send-email-in-bulk-button" value="Send Email In Bulk">48 </p>49 </form>50 <div id="service-response" hidden>51 </div>52</div>53 54<script language="JavaScript">55 const XHR = new XMLHttpRequest();56 XHR.onload = han57</script>58</body>59</html>Sample Service Code - emailService.ss
The following code serves as the backend service that processes the email form submissions:
1/**2 * @NApiVersion 2.x3 * emailService.ss4 */5"text-purple-400">define(['./emailHandler.js', 'N/https'],"text-purple-400">function(emailHandler, https){6 "text-purple-400">function service(context) {7 "text-purple-400">if(context.request.method == https.Method.POST) {8 "text-purple-400">var result = {};9 "text-purple-400">var emailData = JSON.parse(context.request.body);10 emailData.author = -5;11 emailData.recipients = convertToArray(emailData.recipients);12 emailData.cc = convertToArray(emailData.cc);13 emailData.bcc = convertToArray(emailData.bcc);14 15 "text-purple-400">try {16 switch(emailData.emailtype) {17 case "Send Email":18 result.response = emailHandler.sendSingleEmail(emailData);19 break;20 case "Send Email In Bulk":21 result.response = emailHandler.sendBulkEmail(emailData);22 break;23 default:24 "text-purple-400">throw {name: "NotImplementedError", message: "Unknown email type!"};25 }26 }27 "text-purple-400">catch(e) {28 result.error = e.name + ': ' + e.message;29 }30 31 context.response.write(JSON.stringify(result));32 }33 }34 35 "text-purple-400">function convertToArray(input)36 {37 "text-purple-400">if(input) {38 "text-purple-400">return input.replace(/\s/g,'').split(",");39 }40 }41 42 "text-purple-400">return{43 service : service44 };45});Key Takeaways
- Begin every SuiteScript 2.0 SSP file with
<%@ NApiVersion="2.x" %>. - Use
.sspfor entry points and.ssfor services in SuiteScript application development. - Structure your email forms clearly and ensure optional and mandatory fields are well defined.
Source: This article is based on Oracle's official NetSuite documentation.
Frequently Asked Questions (4)
Do I need a specific directive when starting a SuiteScript 2.0 SSP file?
Is it possible to use the @NScriptType annotation in .ss or .ssp files?
How does the emailService.ss file handle different email sending modes?
What could cause an SSP file to be mistakenly treated as SuiteScript 1.0?
Was this article helpful?
More in SuiteScript
- Scheduling Map/Reduce Script Submissions in NetSuite
Learn how to schedule map/reduce scripts for one-time or recurring submissions in NetSuite, enhancing automation and efficiency.
- API Governance Units Calculation in NetSuite 2026.1
NetSuite 2026.1 introduces examples illustrating API governance unit calculations for both user event and scheduled scripts.
- Binary File Support in N/https Module for SuiteScript
SuiteScript enhances capabilities with binary file support in the N/https module, allowing improved data handling in external communications.
- Attach and Detach Operations in NetSuite 2026.1
Attach and detach operations for record relationships in NetSuite enhance data management and connectivity.
