SuiteScript 2.x Client Script Type

Client scripts are scripts that are executed by predefined event triggers in the client browser. They can validate user-entered data and auto-populate fields or sublists at various form events. For details, see SuiteScript 2.x Client Script Entry Points and API.

Scripts can be run on most standard records, custom record types, and custom NetSuite pages such as Suitelets. See SuiteScript Supported Records for a list of NetSuite records that support SuiteScript.

Important:

Client scripts only execute in edit mode. If you have a deployed client script with a pageInit entry point, that script does not execute when you view the form. It executes when you click Edit.

The following triggers can run a client script:

Record-level client scripts are executed after any existing form-based clients are run, and before any user event scripts are run.

See Script Type Usage Unit Limits for details about client script governance.

Tip:

You can set the order in which client scripts execute on the Scripted Records page. See The Scripted Records Page.

You can use SuiteCloud Development Framework (SDF) to manage client scripts as part of file-based customization projects. For information about SDF, see SuiteCloud Development Framework. You can use the Copy to Account feature to copy an individual client script to another of your accounts. Each client script page has a clickable Copy to Account option in the upper right corner. For information about Copy to Account, see Copy to Account.

You can use SuiteScript Analysis to learn about when the script was installed and how it performed in the past. For more information, see Analyzing Scripts.

For additional information about SuiteScript 2.x client scripts, see the following:

SuiteScript Client Script Sample

For help with writing scripts in SuiteScript 2.x, see SuiteScript 2.x Hello World and SuiteScript 2.x Entry Point Script Creation and Deployment.

The following sample shows a client script applied to a sales order form. The script performs the following tasks:

          /**
 *@NApiVersion 2.x
 *@NScriptType ClientScript
 */
define(['N/error'], function(error) {
    function pageInit(context) {
        if (context.mode !== 'create')
            return;
        var currentRecord = context.currentRecord;
        currentRecord.setValue({
            fieldId: 'entity',
            value: 107
        });
    }
    function saveRecord(context) {
        var currentRecord = context.currentRecord;
        if (!currentRecord.getValue({fieldId: 'entity'}) || currentRecord.getLineCount({sublistId: 'item'}) < 1)
            throw error.create({
                name: 'MISSING_REQ_ARG',
                message: 'Please enter all the necessary fields on the salesorder before saving'
             });
        return true;
    }
    function validateField(context) {
        var currentRecord = context.currentRecord;
        var sublistName = context.sublistId;
        var sublistFieldName = context.fieldId;
        var line = context.line;
        if (sublistName === 'item') {
            if (sublistFieldName === 'quantity') {
                if (currentRecord.getCurrentSublistValue({
                    sublistId: sublistName,
                    fieldId: sublistFieldName
                }) < 3)
                    currentRecord.setValue({
                        fieldId: 'otherrefnum',
                        value: 'Quantity is less than 3'
                    });
                else
                    currentRecord.setValue({
                        fieldId: 'otherrefnum',
                        value: 'Quantity accepted'
                    });
                }
            }
       return true;
    }
    function fieldChanged(context) {
        var currentRecord = context.currentRecord;
        var sublistName = context.sublistId;
        var sublistFieldName = context.fieldId;
        var line = context.line;
        if (sublistName === 'item' && sublistFieldName === 'item')
            currentRecord.setValue({
                fieldId: 'memo',
                value: 'Item: ' + currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'item'
                }) + ' is selected'
            });
    }
    function postSourcing(context) {
        var currentRecord = context.currentRecord;
        var sublistName = context.sublistId;
        var sublistFieldName = context.fieldId;
        var line = context.line;
        if (sublistName === 'item' && sublistFieldName === 'item')
            if (currentRecord.getCurrentSublistValue({
                sublistId: sublistName,
                fieldId: sublistFieldName
            }) === '39')
                if (currentRecord.getCurrentSublistValue({
                    sublistId: sublistName,
                    fieldId: 'pricelevels'
                }) !== '1-1')
                    currentRecord.setCurrentSublistValue({
                        sublistId: sublistName,
                        fieldId: 'pricelevels',
                        value: '1-1'
                    });
    }
    function lineInit(context) {
        var currentRecord = context.currentRecord;
        var sublistName = context.sublistId;
        if (sublistName === 'partners')
            currentRecord.setCurrentSublistValue({
                sublistId: sublistName,
                fieldId: 'partner',
                value: '55'
            });
    }
    function validateDelete(context) {
        var currentRecord = context.currentRecord;
        var sublistName = context.sublistId;
        if (sublistName === 'partners')
            if (currentRecord.getCurrentSublistValue({
                sublistId: sublistName,
                fieldId: 'partner'
            }) === '55')
                currentRecord.setValue({
                    fieldId: 'memo',
                    value: 'Removing partner sublist'
                });
        return true;
    }
    function validateInsert(context) {
        var currentRecord = context.currentRecord;
        var sublistName = context.sublistId;
        if (sublistName === 'partners')
            if (currentRecord.getCurrentSublistValue({
                sublistId: sublistName,
                fieldId: 'contribution'
            }) !== '100.0%')
                currentRecord.setCurrentSublistValue({
                    sublistId: sublistName,
                    fieldId: 'contribution',
                    value: '100.0%'
                });
        return true;
    }
    function validateLine(context) {
        var currentRecord = context.currentRecord;
        var sublistName = context.sublistId;
        if (sublistName === 'partners')
            if (currentRecord.getCurrentSublistValue({
                sublistId: sublistName,
                fieldId: 'contribution'
            }) !== '100.0%')
                currentRecord.setCurrentSublistValue({
                    sublistId: sublistName,
                    fieldId: 'contribution',
                    value: '100.0%'
                });
        return true;
    }
    function sublistChanged(context) {
        var currentRecord = context.currentRecord;
        var sublistName = context.sublistId;
        var op = context.operation;
        if (sublistName === 'item')
            currentRecord.setValue({
                fieldId: 'memo',
                value: 'Total has changed to ' + currentRecord.getValue({fieldId: 'total'}) + 
                       'with operation: ' + op
            });
    }
    
    return {
        pageInit: pageInit,
        fieldChanged: fieldChanged,
        postSourcing: postSourcing,
        sublistChanged: sublistChanged,
        lineInit: lineInit,
        validateField: validateField,
        validateLine: validateLine,
        validateInsert: validateInsert,
        validateDelete: validateDelete,
        saveRecord: saveRecord
    };
}); 

        

Related Topics

General Notices