Custom Module Examples

The following examples demonstrate using the define Object and require Function when working with custom modules.

Define a custom utility module

The following file holds the definition of a custom module with a custom API. For example, to allow multiple scripts that depend on an incremental counter utility to require and import this functionality.

To protect against version incompatibility, this script includes 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 a native SuiteScript module and a custom module.

The module’s file path is used to pass in the custom utility as a dependency. As a best practice, it does not include the .JS file 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 define a bundled custom module that can be exposed to third parties, you must add the @NModuleScope JSDoc tag and assign it the value public.

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

        

And then use the define Object so that your custom module is recognized as an AMD module. To use a module that is bundled within an external SuiteApp, you need to pass the bundle’s file path, containing a valid bundle ID, within the define() function as follows:

          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 register as AMD compatible in which case, you can specify a require configuration that sets up the path where the module is found.

For example, you can create a JSON configuration file (JsLibraryConfig.json) containing the following code and save it in the File Cabinet:

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

        

Then, you could use the @NAmdConfig JSDoc tag to provide a relative path from the script that needs the coolthing module:

          /**
* @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'
                });
            });
        }
    };
}); 

        

Similarly, to use a version of jQuery, you can form your required configuration as follows.

First, add a JSON configuration file (jQueryConfig.json) to the File Cabinet and configure the path:

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

        

Then, from the script using the jQuery module, reference the path to your JSON configuration file 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 that is attached to the Suitelet using Form.clientScriptFileId or Form.clientScriptModulePath.

Important:

SuiteScript does not support direct access to the NetSuite UI through the Document Object Model (DOM). The NetSuite UI should only be accessed using SuiteScript APIs.

Add a non-AMD library

In this example, the following file would be uploaded to the File Cabinet as a JavaScript file titled math.js in the /SuiteScripts/ShimExample folder. It does not register as 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;
    },
} 

        

When the library does not register as an AMD module, you need to use a require configuration that specifies the configuration parameters in JSON format. In this case, the shim parameter and paths parameter are configured in the MathConfig JSON file and also stored in the ShimExample folder in the File Cabinet:

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

        

The module can be loaded as a dependency in an entry point script, such as in a user event script, provided that a valid @NAmdConfig JSDoc tag with a path to the JSON configuration file is included.

          /**
* @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