Instantiating a Custom Plug-in Script in SuiteScript 1.0

Important:

To create your own custom plug-ins and implementation scripts, you need some JavaScript 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.1.

Instantiating a Custom Plug-in Implementation

When you create a Custom Plug-in Type record in Adding the Default Implementation to NetSuite, you enter a Class Name for the custom plug-in. Before you can call an implementation’s functions, you must use the class name to create a new instance of the implementation you want to use.

Note:

The term “class name” is a misleading. When you create a new instance of an implementation, you're instantiating a delegate, not an object.

Instantiate the implementation using the new keyword and the Class Name from the custom plug-in type record. The constructor has one optional parameter, implementationId, which determines which implementation you instantiate. See Instantiating a Specific Implementation and Instantiating an Implementation Based on the Deployment Model for the available arguments.

          var a = new <Class Name>(implementationId); 

        
Important:

You can instantiate active implementations only after the plug-in is installed by a customer and the plug-in script is executing.

There are two techniques you can use to instantiate an implementation:

Instantiating a Specific Implementation

Pass one of the following arguments for implementationId to instantiate a specific implementation.

Instantiating an Implementation Based on the Deployment Model

Pass one of the following arguments for implementationId to instantiate the implementations that are currently active in the customer’s account. The argument you use depends on the value set for the Deployment Model field. See Adding the Default Implementation to NetSuite for additional information.

Calling a Default Implementation’s Functions

          function useCustomPlugInType()
{
    var a = new DemoPlugInType('default');
    a.doTheMagic({
        operand1: 10,
        operand2: 20
    });
    a.otherMethod();
} 

        

Calling an Alternate Implementation’s Functions

          function useCustomPlugInType()
{
    var b = new DemoPlugInType('customscriptimplementation1');
    b.doTheMagic();
    b.otherMethod();
} 

        

Calling Multiple Implementations’ Functions

In this example, you, the solution provider, create a default implementation and an alternate implementation for the plug-in. The alternate implementation is called by passing the ID from the New Plug-in Implementation record as an argument.

Make sure that the Deployment Model on the custom plug-in type record is set to Allow Multiple.

          function useCustomPlugInType()
{
    var a = new DemoPlugInType('default');
    a.doTheMagic({
        operand1: 10,
        operand2: 20
    });
    a.otherMethod();

    var b = new DemoPlugInType('customscriptimplementation1');
    b.doTheMagic();
    b.otherMethod();
} 

        

Discovering Active Implementations

After your custom plug-in is installed on a customer’s account, the custom plug-in script doesn't directly know about the active implementations. If you set the Deployment Model field on the Custom Plug-in Type record to Allow Single, this is not an issue. See Instantiating an Implementation Based on the Deployment Model for additional information.

If the Deployment Model field on the Custom Plug-in Type record is set to Allow Multiple and you don't specify which implementation to instantiate, you'll need to use the getImplementations method to instantiate the implementations. The getImplementations method returns an array of active implementations.

          function discoverImplementations()
{
    var x = DemoPlugInType.getImplementations();
    for (var i = 0; i < x.length; i++)
    {
        var t = new DemoPlugInType(x[i]);
        t.doTheMagic();
        t.otherMethod();
    }
} 

        

Related Topics

General Notices