Instantiating a Custom Plug-in Script in SuiteScript 2.x
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:
-
Using SuiteScript, pass the script implementation ID to the
implementation
parameter of theplugin.loadImplementation
function. -
Using the Manage Plug-ins page, instantiate the implementation that is currently active in the customer’s account.
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:
-
Use the plugin.findImplementations function to count the total number of active implementations and get a list of all the implementation IDs for a custom plug-in script type ID named customscript_magic_plugin.
-
Traverse each implementation, instantiating them with the plugin.loadImplementation function and running doTheMagic, a function that is defined in each implementation.
/**
* @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:
-
Go to Customization > Plug-ins > Manage Plug-ins.
-
Locate your custom plug-in name, and then select the desired active plug-in from the list of implementations.
-
Click Save.
Related Topics
- Custom Plug-in Overview
- Custom Plug-in Creation
- Bundling a Custom Plug-in
- Creating a Custom Plug-in Interface
- Creating the Default Implementation for a Custom Plug-in
- Adding the Default Implementation to NetSuite
- Creating a Custom Plug-in Alternate Implementation
- Adding an 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