• : 1-877-596-8218
  • : sales@aecc.ca
# # #
#

SuiteScript 2.0.

Version 2.0 of SuiteScript allows developers to use modules to better structure their code. For example, you load a message module if you need to display a message at the top of the screen.

There are two main functions in SuiteScript 2.0: define and require. When you use “define” the system loads all modules before it executes any logic, “require” loads modules only when they are necessary.

There are different types of modules to use: email, file, record, http. The list of all modules can be found in the documentation: SuiteCloud -> SuiteScript 2.0 -> SuiteScript 2.0 API -> SuiteScript 2.0 Modules.

The following script types are supported:

1) Client Script Type
2) User Event Type
3) Scheduled Script Type
4) RESTlet Script Type
5) Map/Reduce Script Type
6) Mass Update Script Type
7) …

Here we are going to discuss two script types: User Event and Client Script.

User event script

User Event Script Types are used when users are working with records: create, save, update, open. Such scripts are located on the server side. Here is an example of how to use user event SuiteScript 2.0:


/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */
define(['N/record'], function (record) {
        function doSomething1(context) {
            if (context.type !== context.UserEventType.CREATE)
                return;
            var customerRecord = context.newRecord;
            customerRecord.setValue('phone', '555-555-5555');
            if (!customerRecord.getValue('salesrep'))
                customerRecord.setValue('salesrep', 46);
        }
        function doSomething2(context) {
            if (context.type !== context.UserEventTypes.CREATE)
                return;
            var customerRecord = context.newRecord;
            customerRecord.setValue('comments', 'Please follow up with this customer!');
        }
        function doSomething3(context)  {
            if (context.type !== context.UserEventTypes.CREATE)
                return;
            var customerRecord = context.newRecord;
            if (customerRecord.getValue('salesrep')) {
                var call = record.create({
                    type: record.Type.PHONE_CALL,
                    isDynamic: true
                });
                call.setValue('title', 'Make follow-up call to new customer');
                call.setValue('assigned', customerRecord.getValue('salesrep'));
                call.setValue('phone', customerRecord.getValue('phone'));
                try {
                    var callId = call.save();
                    log.debug('Call record created successfully', 'Id: ' + callId);
                } catch (e)  {
                    log.error(e.name);
                }
            }
        }
        return {
            beforeLoad: doSomething1,
            beforeSubmit: doSomething2,
            afterSubmit: doSomething3
        };
    });

The header of the file is mandatory. If you don’t put the comment on top of the file the NetSuite won’t understand that this code is actually a user event script. “define” function has two parameters: first one is an array of modules and the second is a callback. The callback function must return an object with defined functions you want to use in your script. For example, if you need only beforeLoad functionality then use only this function in returned object:


return {
       beforeLoad: doSomething1
}

Each internal function (like doSomething1) gets a context as a parameter. From the context you can extract current/previous version of the object or the type of the operation (CREATE, EDIT, DELETE).

SuiteScript 2.0 allows the developer works with records using more concise code. If in the previous version (SuiteScript 1.0) to update a field you had to use something like nlapiSetFieldValue function, in SuiteSript 2.0 you can simply use, for example, currentRecord.setValue(‘fieldName’, fieldValue).

Client side script

Client scripts are scripts that are working on the browser side. You can use them to validate fields, to execute some logic before you save record. The following events can trigger the script:

1) Form saving
2) Form initialization
3) Selection of line item
4) Adding a line item
5) Change/Enter a value in a field

The next example shows how to use a client side (browser side) SuiteScript 2.0:


/**
 * @NApiVersion 2.x 
 * @NScriptType ClientScript
 */
define(['N/ui/message'], function(msg) {
        function showErrorMessage(msgText) {
            var myMsg = msg.create({
                title: "Cannot Save Record",
                message: msgText,
                type: msg.Type.ERROR
            });
            myMsg.show({
                duration: 5000
            });
        }
        function saveRec(context) {
            var rec = context.currentRecord;
            var currentDate = new Date()
            var oneWeekAgo = new Date(currentDate - 1000 * 60 * 60 * 24 * 7);
            // Validate transaction date is not older than current time by one week
            if (rec.getValue({
                fieldId: 'trandate'
            }) < oneWeekAgo) {
                showErrorMessage("Cannot save sales order with trandate one week old.");
                return false;
            }
            //validate total is greater than 0
            if (rec.getValue({
                fieldId: 'total'
            }) <= 0) {
                showErrorMessage("Cannot save sales order with negative total amount.");
                return false;
            }
            return true;
        }

        return {
            saveRecord: saveRec
        }
    });

The header is also mandatory for the client side script. The return value of the callback is also an object that contains functions the developer want to use. It’s possible to use the following functions: pageInit, saveRecord, validateField and so on (all list can be found in the documentation).