SuiteScript 2.0 Basics

NetSuite for Developers

If you’re following along with this NetSuite Development series, you now have a good understanding of the more common NetSuite script types. Before jumping into some more specifics about any one of these types, I want to give an overview of the basic components of a SuiteScript 2.0 script. I will also be sharing some good resources that will be a huge help in your future scripting projects. So let’s dive in!

SuiteScript 2.0 Main Elements

Define Modules to Use

Suitescript 2.0 has a modular architecture. All that means is that the API is broken down into modules, and you have to tell every script which ones you will be using. For example, if you need to do some things with a record like loading a record, getting the value of a field on a record, or setting a value on a record, you will need the “record” module, and if you need to create a scripted search you will need the “search module.” Here is how it looks to include those modules in your script:

define(['N/record','N/search'], function (record, search) {
    //everything else will go inside here
});

Script Version and Script Type

The next fundamental part of every SuiteScript 2.0 script is a section that defines the API version and the script type. This is important because NetSuite will look for that section when you upload the script, and that’s how it knows what script type and version your script is. After you add that section to your script, this is what your script should look like:

define(['N/record','N/search'], function (record, search) {
    /**
    *@NApiVersion 2.0
    *@NScriptType ClientScript
    */
    
    //everything else will go in here
});

Script Functions and Triggers

Now you will have to add a function for every “trigger” that is needed. For instance, with this client script you may want to do something every time a field changes, and you may want to check on something every time you click the “save” button. In that case, you would need functions for when a field is changed and for when the record is saved. This is what that would look like:

    define(['N/record','N/search'], function (record, search) {
    /**
    *@NApiVersion 2.0
    *@NScriptType ClientScript
    */
    
    function fieldChanged(context) {
        // do things
    }
    
    function saveRecord(context) {
        // do things
    }
    
});

After you add in your functions for each trigger, you have to return an object (right now you don’t need to know what an object actually is) that will tell NetSuite what function to use for each particular trigger. Here’s what that looks like:

    define(['N/record','N/search'], function (record, search) {
    /**
    *@NApiVersion 2.0
    *@NScriptType ClientScript
    */
    
    function fieldChanged(context) {
        // do things
    }
    
    function saveRecord(context) {
        // do things
    }
    
    return {
        fieldChanged: fieldChanged,
        saveRecord: saveRecord
    }
    
});

Conclusion

Those are the bare minimum elements that you will need to include when developing any script with SuiteScript 2.0. In later blogs we will go into more detail and add some things to the script to make it come alive and actually do something in NetSuite! You can access the next blog in this series here; and don’t forget to subscribe to our mailing list so you can receive more NetSuite content directly in your inbox each week!