Custom Module Examples

The examples below show how to use the define Object and require Configuration

Define a custom utility module

The following file defines a custom module with its own API. For example, you can let multiple scripts use an incremental counter by requiring and importing this module.

To avoid version issues, this script uses the @NApiVersion tag.

          /**
 * counter.js
 * @NApiVersion 2.1
 */
define(function(){
    let counter = 0;

    function incrementValue() { 
        counter++;
    }
    function getValue() { 
        return counter;
    }

    return {
        increment: incrementValue,
        value: getValue
    }
}); 

        

Import the custom utility module

This example uses the define Object to include both a built-in SuiteScript module and a custom module.

It uses the module’s file path to include the utility as a dependency—best practice is to skip the .js extension.

          /**
 * customRecord.js
 * @NApiVersion 2.x
 */
define(['N/record','./counter'],
    function(record,counter){
        function createCustomRec() {
            record.create(…);
            counter.increment();
    }

    return {
        createCustomRecord: createCustomRec
    }
}); 

        

Define a custom module for a SuiteApp

To make a bundled custom module available to third parties, add the @NModuleScope JSDoc tag and set it to public.

          /**
 * @NApiVersion 2.x
 * @NModuleScope public
 */ 

        

Then use define Object so your module is treated as an AMD module. To use a module bundled inside an external SuiteApp, pass the bundle’s file path with its bundle ID to define(), like this:

          define(['/.bundle/<bundle ID>/<module path>'],
    function(<module name>){
        <logic goes here>
    }
); 

        

Import a third-party JavaScript library

There are two types of imports for third-party libraries:

Import a third-party library

Some third-party libraries are AMD compatible, so you can set a require config with the module’s path.

For example, create a JsLibraryConfig.json file with this code and save it in the File Cabinet:

          ...
{
  "paths": {
    "coolthing": "/SuiteScripts/myFavoriteJsLibrary"
  }
}
... 

        

Next, use the @NAmdConfig JSDoc tag to set the relative path from your script to the config file:

          /**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NAmdConfig  ./JsLibraryConfig.json 
*/

define(['coolthing'],
    function (coolthing) {
        return {
            beforeLoad: function beforeLoad(ctx) {
                coolthing.times(2, function () {
                    log.debug({
                        title: 'log', 
                        details: 'log'
                });
            });
        }
    };
}); 

        

You can do something similar to use a specific version of jQuery:

First, add jQueryConfig.json to the File Cabinet and set the path:

          ...
{
  "paths": {
    "jquery": "/SuiteScripts/myjQueryLib"
  }
}
... 

        

Then in your script, reference the path to the JSON config in the @NAmdConfig JSDoc tag. For example: * @NAmdConfig /SuiteScripts/myjQueryLib.json.

          ...
* @NAmdConfig  ./jQueryConfig.json 
*/
 
define(['jquery'], function(jquery) {
});
... 

        
Note:

If you want to use jQuery with a Suitelet, import it using an on-demand client script attached with Form.clientScriptFileId or Form.clientScriptModulePath.

Important:

SuiteScript doesn’t support direct DOM access to the NetSuite UI—only use SuiteScript APIs.

Add a non-AMD library

In this example, upload math.js to /SuiteScripts/ShimExample—it’s not AMD compatible.

          var myMath =  {
    add: function(num1, num2) {
        return num1 + num2;
    },
    subtract: function(num1, num2) {
        return num1 - num2;
    },
    multiply: function(num1, num2) {
      return num1 * num2;
    },
    divide: function(num1, num2) {
        return num1 / num2;
    },
} 

        

If the library isn’t AMD compatible, use a require config with JSON parameters. Here, you add shim and paths parameters in MathConfig.json, also saved in ShimExample.

          ...
{
    "paths": {
        "math": "SuiteScripts/ShimExample/math.js"
    },
    "shim":{
        "math": {
            "exports": "myMath"
        }
    }
}
... 

        

You can then load the module as a dependency in an entry point script (like a user event script) as long as you include a valid @NAmdConfig JSDoc tag with the path to the JSON config.

          /**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NAmdConfig  /SuiteScript/ShimExample/MathConfig.json
*/
 
define(['math'], function (math) {
    return {
        beforeLoad: function beforeLoad() {
            log.debug({
                title: 'test', 
                details: myMath.add(1,2)
            });
        }
    }
}); 

        

Related Topics

General Notices