Netsuite's client script

Hi there,

Thanks for visiting Netsuite Guru,
In this topic we are going to learn about Netsuite's Client Script.
Client script also trigger on some actions but on Client side or we can say on browser.
Client side is similar to what we call client side validation in HTML/Javascript.
Example:
1. On loading of form for editing
2. Validate data before or after entering in field
3. Adding line
4. Saving form and many more other triggering points.

Client Script Entry Points:

1. fieldChanged :  Executed when a field is changed by a user or client side call.
2.  lineInit : Executed when an existing line is selected.
3. pageInit : Executed when the page completes loading or when the form is reset.
4. postSourcing : Executed on transaction forms when a field that sources information from another field is modified.
5. saveRecord : Executed after the submit button is pressed but before the form is submitted.
6. sublistChanged: Executed after a sublist has been inserted, removed, or edited.
7. validateDelete : Executed when removing an existing line from an edit sublist.
8. validateField: Executes when a field is about to be changed by a user or client side call.
9. validateInsert : Executed when you insert a line into an edit sublist.
10. validateLine : Executed before a line is added to an inline editor sublist or editor sublist.

*** Total usage limit allowed for client script is 1,000 per script.
*** If user with restricted access uses form with client script, client script throws error message while accessing records which are not allowed for current role. So write your script accordingly.

Current Record Module:

Client script uses current record module for validating data from current record in Netsuite Form.
You can use the currentRecord module in the following types of scripts:
Entry point client scripts: These scripts are standard client script, use the @NScriptType ClientScript annotation. They have all entry point defined in script.

Client-side custom modules : These are add-on, enhanced, and custom built for some purpose. These type of script usually work with form and other CS script to enhance functionality.
**** We will write on Current Module in detail in future.Please keep visiting our blog.

Note : When debugging client-side scripts, you can insert a debugger; statement in your script, and execution will stop when this statement is reached:
function hello() { var ng = 1; ng *= (ng + 9); debugger; console.log(ng); }
Using N/record module you can load/edit/create records but it is advisable to use this wisely as this will slow down page speed.
Example: Never write a CS which create/update record on field change or on saving because saving does not guarantee that changes are in Database now. Form saving may get fail because of beforSubmit of user event but CS already created a record in Netsuite on field change or Save. If create/update is on field change, user may change field for many time and each change result into create/update of record.

How to create Client Script in Netsuite?

Step 1: Create javascript file, use our sample code below which covers all functions.
how to create client script file
Customization>Scripting>Scripts>New

Step 2: Click on Create Script Record button
Step 3: Enter Script name, id and other details
how script page looklike for client script
All functions will be populate from script file.

Step 4: Click on Save and Deploy button
Step 5: Enter details on Deployment page
how client script deplyment looklike in netsuite
Enter details on deployment page
Step 6: Save.

Entry Points in Sample Code Template:

/**
 *@NApiVersion 2.x
 *@NScriptType ClientScript
 */
define(['N/error'],
    function(error) {
        function ngPageInit(context) {
            if (context.mode !== 'create')
                return;
            var currentRecord = context.currentRecord;
            currentRecord.setValue({
                fieldId: 'custbody_ngfield',
                value: 111
            });
        }
        function ngSaveRecord(context) {
            var currentRecord = context.currentRecord;
            // Write your saveRecord code here
            return true;
        }
        function ngValidateField(context) {
            var currentRecord = context.currentRecord;
            var sublistName = context.sublistId;
            var sublistFieldName = context.fieldId;
            var line = context.line;
            if (sublistName === 'item') {
                if (sublistFieldName === 'quantity') {
                   // write your validateField logic here.
                   // sublistName and sublistFieldName is just for reference, you can add other sublist and field here.
                }
            }
            return true;
        }
        function ngFieldChanged(context) {
            var currentRecord = context.currentRecord;
            var sublistName = context.sublistId;
            var sublistFieldName = context.fieldId;
            var line = context.line;
            if (sublistName === 'item' && sublistFieldName === 'item')
            {
                //write your fieldChange Logic Here
            }
        }
        function ngPostSourcing(context) {
            var currentRecord = context.currentRecord;
            var sublistName = context.sublistId;
            var sublistFieldName = context.fieldId;
            var line = context.line;
            if (sublistName === 'item' && sublistFieldName === 'item')
            {
               //write your postSourcing Logic here
            }
        }
        function ngLineInit(context) {
            var currentRecord = context.currentRecord;
            var sublistName = context.sublistId;
            if (sublistName === 'item')
            {
                //write your lineInit logic here.
            }
        }
        function ngValidateDelete(context) {
            var currentRecord = context.currentRecord;
            var sublistName = context.sublistId;
            if (sublistName === 'item')
            {
                // write you validateDelete logic here
            }
            return true;
        }
        function ngValidateInsert(context) {
            var currentRecord = context.currentRecord;
            var sublistName = context.sublistId;
            if (sublistName === 'item')
            {
                // write your validateInsert logic here
            }
            return true;
        }
        function ngValidateLine(context) {
            var currentRecord = context.currentRecord;
            var sublistName = context.sublistId;
            if (sublistName === 'item')
            {
                //currentRecord.getCurrentSublistValue
            //currentRecord.setCurrentSublistValue
               // add your validateLine logic here
            }
            return true;
        }
        function ngSublistChanged(context) {
            var currentRecord = context.currentRecord;
            var sublistName = context.sublistId;
            var op = context.operation;
            if (sublistName === 'item')
            {
                //currentRecord.setValue
            //currentRecord.getValue
            // add your sublistChanged logic here.
            }
        }
        return {
            pageInit: ngPageInit,
            fieldChanged: ngFieldChanged,
            postSourcing: ngPostSourcing,
            sublistChanged: ngSublistChanged,
            lineInit: ngLineInit,
            validateField: ngValidateField,
            validateLine: ngValidateLine,
            validateInsert: ngValidateInsert,
            validateDelete: ngValidateDelete,
            saveRecord: ngSaveRecord
        };
    });


We hope this post help you in understanding client script, share with your friend, comment below if you have any question or suggestion.

Thanks
Netsuite Guru


Download Netsuite Guru Android App
Follow Me on Linkedin
Our FB Page

Comments