define(id, [dependencies,] moduleObject)

Method Description

Function used to create entry point scripts and custom modules in SuiteScript 2.x. For more information, see SuiteScript 2.x Entry Point Script Creation and Deployment and SuiteScript 2.x Custom Modules.

Use this define() signature if your entry point script or custom module has required dependencies.

If you are creating an entry point script, the define() function must return an object consisting of at least one key:value pair. Each key must be an entry point and the corresponding value must be a named entry point function. All entry points must be for the same script type. Your script can have only one entry point script and the entry point script must be only one script type. Your entry point script can, however, load multiple custom modules as dependencies. There is no limit to the number of dependencies your entry point script can load.

Returns

Object

Global object

define Object

Since

2015.2

Parameters

Parameter

Type

Required / Optional

Description

Since

id

string

optional

Defines the id of the module.

2015.2

dependencies

string []

optional

Represents all module dependencies required by the callback function.

Use the following syntax:

  • Native SuiteScript 2.x modules: [‘N/<module name>’]

  • Custom modules: [/‘<path to module file in File Cabinet>/<module name>’]

    For other options, see Module Dependency Paths.

2015.2

moduleObject

Function | Object

required

A callback function or a module object

2015.2

Errors

Error Code

Message

Thrown If

MODULE_DOES_NOT_EXIST

Module does not exist: {module path/name}

The NetSuite module or custom module listed as a dependency does not exist.

Note that NetSuite only reports the first error encountered. If you have multiple dependent modules and you receive this error, verify that all module paths and names are correct.

Syntax for Module ID

The following code shows sample syntax for the define(id, [dependencies,] callback) function signature. These code samples are not functional examples and do not provide complete list of ways this define() signature can be used.

          ...
define('mymodule', ['/test', '/sample'], function(test, sample){...});
... 

        
Syntax for Entry Point Script

The following example is a SuiteScript user event script type that creates a Phone Call record on the afterSubmit trigger.

          /**
*@NApiVersion 2.x
*@NScriptType UserEventScript
 */

define(['N/record'],
    function (record)
    {
        function createPhoneCall(context)
        {
            if (context.type !== context.UserEventTypes.CREATE)
                return;
            var customerRecord = context.newRecord;
            if (customerRecord.getValue('salesrep'))
            {
                var phoneCall = record.create({
                    type: record.Type.PHONE_CALL,
                    isDynamic: true
                });
                phoneCall.setValue({
                    fieldId: 'title', 
                    value: 'Make follow-up call to new customer'
                });
                phoneCall.setValue('assigned', customerRecord.getValue('salesrep'));
                phoneCall.setValue('phone', customerRecord.getValue('phone'));
                try
                {
                    var callId = phoneCall.save();
                    log.debug({
                        title: 'Call record created successfully', 
                        details: 'Id: ' + callId
                    });
                }
                catch (e)
                {
                    log.error(e.name);
                }
            }
        }
        return {
            afterSubmit: createPhoneCall
        };
    }); 

        
Syntax for Custom Module

The following code shows the syntax for creating a custom SuiteScript 2.x module in the script file lib.js.

          // lib.js
define(['./api/bar'], function(bar){      // require bar custom module 
    return {
        makeSomething: function(){        // define function lib.makeSomething()
            var barObj = bar.create();    // use create() function from bar custom module
            return bar.convertToThing();  // returns the value of bar module function convertToThing()
        }
    }
}); 

        

The following code shows the syntax for calling the function lib from the custom module test.js in a separate script file:

          // test.js
require(['/lib'], function (lib) {   // require custom module (defined above)
   
     return lib.makeSomething();     // return value of makeSomething function in custom module 
   
}); 

        

Related Topics

General Notices