NetSuite SuiteScript Development
Introduction to SuiteScript
What is SuiteScript?
NetSuite is a powerful tool for running a business, but every business is unique. That's where SuiteScript comes in. It's NetSuite's special toolkit that lets you customize and extend the platform to fit your exact needs. Think of it as a way to teach NetSuite new tricks.
At its core, SuiteScript is built on JavaScript, one of the most popular programming languages in the world. This means if you have some experience with web development, you'll feel right at home.
With SuiteScript, you can automate repetitive tasks, create custom workflows, build entirely new applications within NetSuite, and connect NetSuite to other software. It's the key to unlocking the full potential of the platform and making it work for you, not the other way around.
The Evolution from 1.0 to 2.x
SuiteScript has been around for a while, and it has evolved significantly. The original version, SuiteScript 1.0, was a procedural, function-based language. It got the job done, but as NetSuite and the web grew more complex, a more modern approach was needed.
Enter SuiteScript 2.x. This isn't just a minor update; it's a complete redesign that aligns with modern JavaScript development practices. It introduces a modular architecture, which makes code cleaner, more reusable, and easier to manage.
| Feature | SuiteScript 1.0 | SuiteScript 2.x |
|---|---|---|
| Architecture | Procedural | Modular (AMD) |
| API Style | Global functions (e.g., nlapiCreateRecord) | Modules with methods (e.g., record.create) |
| Syntax | Older JavaScript (ES3) | Modern JavaScript (ES2015+) |
| Code Organization | Single file, less structure | Organized into self-contained modules |
| Readability | Can become hard to follow | Clearer and more maintainable |
While you might still encounter some older 1.0 scripts, all new development should use SuiteScript 2.x. It's more powerful, more efficient, and the standard for NetSuite customization today.
A Look at a 2.x Script
The best way to understand SuiteScript 2.x is to see it in action. Let's look at the basic structure of a simple script. Don't worry about understanding every single detail yet. The goal is to see how the pieces fit together.
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/record', 'N/log'],
function(record, log) {
function beforeLoad(context) {
log.debug({
title: 'Script Triggered',
details: 'The beforeLoad event occurred on record: ' + context.newRecord.id
});
}
return {
beforeLoad: beforeLoad
};
});
This script might look a bit complex, but it follows a clear pattern. Let's break it down:
-
JSDoc Header: The comments at the very top (
/** ... */) are required.@NApiVersiontells NetSuite which version of the API to use (always use2.1for new scripts).@NScriptTypedefines what kind of script this is. In this case, it's aUserEventScript, which runs automatically when a user interacts with a record (like opening, creating, or saving it). -
defineFunction: This is the heart of the modular system. It lists the built-in NetSuite modules the script needs to do its job. Here, we're asking for theN/recordmodule to work with records and theN/logmodule for logging information. -
Callback Function: The
definestatement includes a function that receives the requested modules as arguments (record,log). All your script's logic goes inside this function. -
Entry Points: A script has specific functions that NetSuite calls at certain times. These are called entry points. In our example,
beforeLoadis an entry point that runs right before a record is loaded for a user to see. -
returnStatement: Finally, the script returns an object that maps the entry point names to the functions that handle them. This tells NetSuite exactly which function to run for thebeforeLoadevent.
This modular structure keeps code organized. Instead of having a single massive file, you create small, focused modules that are easier to test, reuse, and understand.
Now that you've seen the basic structure, you're ready to start exploring what you can build with SuiteScript.