Instantiating a Custom Plug-in Script in SuiteScript 2.x

Important:

To create your own custom plug-ins and implementation scripts, you need some JavaScript coding experience and an understanding of SuiteScript. You should write new custom plug-in scripts in SuiteScript 2.0 or SuiteScript 2.1. To get started with SuiteScript, see SuiteScript 2.x API Reference and SuiteScript 2.1.

To instantiate a custom plug-in script implementation in your code, use the plugin.loadImplementation function. For more information, see plugin.loadImplementation(options).

There are two ways to invoke a specific implementation:

Important:

To use the Script Debugger on a script that instantiates a specific implementation, you must add the script that instantiates a Custom Plug-in to NetSuite. For more information, see Adding a Script that Instantiates a Custom Plug-in to NetSuite.

Instantiating a Specific Implementation using SuiteScript

Instantiate a specific implementation by passing the script implementation ID to the implementation parameter of the plugin.loadImplementation function. The ID for the default implementation is default. Use the plugin.findImplementations function to get a list of all the active script implementation IDs.

The following example shows a Suitelet script that performs these tasks:

          /**
 * @NApiVersion 2.x
 * @NScriptType suitelet
 */
define(['N/plugin'], function(plugin) {
    return {
        onRequest: function(options) {
            var impls = plugin.findImplementations({
                type: 'customscript_magic_plugin'
            });
            for (i = 0; i < impls.length; i++) {
                var pl = plugin.loadImplementation({
                    type: 'customscript_magic_plugin',
                    implementation: impls[i]
                });
                options.response.write('impl = ' + impls[i] + ', result = ' + pl.doTheMagic({
                    operand1: 10,
                    operand2: 20
                }) + '\n');
            }
        }
    }
}); 

        

For more information about the plugin.findImplementations function, see plugin.findImplementations(options).

Instantiating a Specific Implementation using the Manage Plug-ins Page

To instantiate a specific implementation using the UI, refrain from specifying the implementation parameter in your plugin.loadImplementation call. When this parameter is not specified, NetSuite runs the active plug-in implementation that is specified by the Manage Plug-ins page.

The following example shows a Suitelet script that runs doTheMagic, a custom plug-in function that is defined in every implementation of the customscript_magic_plugin type. The implementation parameter is not specified.

          /**
 * @NApiVersion 2.x
 * @NScriptType suitelet
 */
define(['N/plugin'], function(plugin) {
    return {
        onRequest: function(options) {
            var pl = plugin.loadImplementation({
                type: 'customscript_magic_plugin'
            });
            options.response.write('impl not specified, result = ' + pl.doTheMagic({
                operand1: 10,
                operand2: 20
            }) + '\n');
        }
    }
}); 

        

To specify which script implementation to run:

  1. Go to Customization > Plug-ins > Manage Plug-ins.

  2. Locate your custom plug-in name, and then select the desired active plug-in from the list of implementations.

  3. Click Save.

Related Topics

Custom Plug-in Overview
Custom Plug-in Creation
Bundling a Custom Plug-in
Creating a Custom Plug-in Interface
Creating a Custom Plug-in Default Implementation
Adding the Default Implementation to NetSuite
Creating a Custom Plug-in Alternate Implementation
Adding the Alternate Implementation to NetSuite
Adding a Script that Instantiates a Custom Plug-in to NetSuite
Instantiating a Custom Plug-in Script in SuiteScript 1.0

General Notices