Module 2: Developing SuiteScripts

Week 1 • NetSuite SuiteScript 2.0 Training • ~45 minutes

🎯 Learning Objectives

1. SuiteScript 2.0 Anatomy

Every SuiteScript 2.0 file has three required parts:

  1. Required Annotations — Tell NetSuite about your script
  2. Define Statement — Import modules and create your script module
  3. Entry Point Functions — The functions NetSuite will call

2. Required Annotations

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
🚫 Critical

Without these annotations, your script won't be recognized as SuiteScript 2.0!

AnnotationPurposeValues
@NApiVersionSuiteScript version2.0 or 2.1
@NScriptTypeScript typeUserEventScript, ClientScript, ScheduledScript, etc.
@NModuleScopeModule visibility (optional)SameAccount, Public
💡 2.0 vs 2.1

Version 2.1 supports modern JavaScript features like arrow functions, let/const, template literals, and promises. Use 2.1 for new development.

3. The Define Statement

The define statement imports NetSuite modules and creates your script module:

define(['N/record', 'N/log'], function(record, log) {
    // Your code here
    
    return {
        afterSubmit: afterSubmit
    };
});

How It Works

PartPurpose
['N/record', 'N/log']Array of modules to import
function(record, log)Parameters that receive the imported modules
return { ... }Object mapping entry point names to functions
⚠️ Order Matters

The order of modules in the array must match the order of parameters in the function. If you import ['N/record', 'N/log'], your function must be function(record, log) — not function(log, record).

4. Entry Point Functions

Entry points are functions that NetSuite calls automatically when certain events occur:

function afterSubmit(context) {
    log.debug('Title', 'Record saved successfully!');
}

Entry Points by Script Type

Script TypeEntry Points
User EventbeforeLoad, beforeSubmit, afterSubmit
Client ScriptpageInit, fieldChanged, validateField, saveRecord, etc.
Scheduledexecute
Map/ReducegetInputData, map, reduce, summarize
SuiteletonRequest
RESTletget, post, put, delete

5. Development Workflow

The complete process to deploy a script:

  1. Write the Script — Create your .js file in Eclipse or any IDE
  2. Upload to File Cabinet — Put the file in SuiteScripts folder
  3. Create Script Record — Customization → Scripting → Scripts → New
  4. Create Deployment — Link script to specific records/triggers
  5. Test — Trigger the script and check Execution Log
💡 Eclipse SuiteCloud IDE

The SuiteCloud IDE plugin automates uploading to File Cabinet. You can also manually upload via Documents → Files → File Cabinet → SuiteScripts.

6. Hello World Example

Here's a complete User Event script that logs a message after every record save:

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 * @description Hello World - logs after record save
 */
define(['N/log'], function(log) {
    
    /**
     * After the record is submitted to the database
     * @param {Object} context
     * @param {Record} context.newRecord - The saved record
     * @param {string} context.type - Trigger type (create, edit, delete)
     */
    function afterSubmit(context) {
        log.debug({
            title: 'Hello World',
            details: 'Script executed! Trigger type: ' + context.type
        });
    }
    
    return {
        afterSubmit: afterSubmit
    };
});

Viewing the Execution Log

After triggering your script, check the output:

  1. Go to Customization → Scripting → Script Deployments
  2. Click on your deployment
  3. Click the Execution Log subtab
  4. Look for your log messages
✅ Log Levels

There are four log levels: log.debug(), log.audit(), log.error(), log.emergency(). Use debug for development, audit for important business events.

🏋️ Practice Exercises

Exercise 1: Your First Script

Create a User Event script on the Employee record that logs the employee's name in afterSubmit. Deploy it and save an Employee to see the log.

Exercise 2: Add More Logging

Modify your script to also log in beforeSubmit. Compare what information is available in beforeSubmit vs afterSubmit.

Exercise 3: Different Log Levels

Add logs at all four levels (debug, audit, error, emergency). Then change the deployment's Log Level setting and observe which logs appear.

🎯 Key Takeaways

  • Every SuiteScript 2.0 needs annotations, define statement, and entry points
  • @NApiVersion and @NScriptType are required annotations
  • Use define() to import modules and create your script
  • Module array order must match function parameter order
  • Scripts must be uploaded to File Cabinet before deployment
  • Create Script Record and Deployment to activate your script
  • Check Execution Log to verify your script ran successfully