How to set Sublist line item value using client script in Netsuite SuiteScript 2.x

 In This post we will learn how to set sublist field value in client script. We will write a code snippet in client script which will trigger on pageinit and close all the lines of sales order item sublist. 

This code is written in Suitescript 2.1

/**
 * @NApiVersion 2.1
 * @NScriptType ClientScript
 */

define(['N/record'], (record)=>{
	
	const pageInit = (context)=>{
		 const currRecord = context.currentRecord;			
		 const lines = currRecord.getLineCount({
			 sublistId: 'item'
		 });
		 
		 for(let i=0; i<lines; i++){
			 
			const lineNum = currRecord .selectLine({
				sublistId: 'item',
				line: i
			});
			 
			currRecord.setCurrentSublistValue({
				sublistId: 'item',
				fieldId: 'isclosed',
				value: true,
				ignoreFieldChange: true
			});
			
			currRecord.commitLine({
				sublistId: 'item'
			});
		 }

		 
	}
	 
		
	return {
		pageInit: pageInit
	};
	
});

As you can see we are using selectline function to select the line first then using setCurrentSublistValue to finally set the field value and now we should not forget to commit the line 

What is difference between require and define in Netsuite SuiteScript 2.x

Require Function

require([dependencies,] callback)

Require Function executes the callback function and loads the dependencies when they are required. On the client, the require function runs asynchronously, and on the server, it runs synchronously.

In require function dependencies are not loaded until they are needed. For example if you included a library as a dependency and it has a method helloworld now when you are calling method from library then only require will load the library dependency and execute the hello world method. 


Define Function

define(id, [dependencies,] moduleObject)

In define function all the dependencies are loaded first before the callback function executes. 

In define function dependencies are not mandatory, for custom modules you do not need any dependency to pass. In define function both id and dependencies are optional and you do not need to pass only moduleObject is mandatory.

If you are making a entry point script define function must return a key value pair for e.g 

define([], function(){

return {

        hello: function(){

            return "hello";

        }

}

});

For suiteScript debugger use require function and not define. Because define doesn't work with debugger. 



How to use Client Script functions like saveRecord, PageInit on a Suitelet form

 In this post we are going to see how we can use standard client script triggers like saveRecord and pageInit on a adhoc client script  that is attached to a suitelet form.

From below code you will find that we can not only use our custom function code in client script but also standard Netsuite functions like saveRecord and pageInit as well along with custom code and this client script is not deployed but it is just in file cabinet of suitescript folder. 

Client Script Code: 

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

define([ 'N/record' ],function(record){
    function pageInit(){
        
	alert("This is PageInit");
    }
  
    function saveRecord(){
      alert("save Record")
      return true;
    }
    
    function customFun(){
      alert("customFun")
    }
    return {
         pageInit: pageInit,
         saveRecord: saveRecord,
         customFun: customFun
    }
});

Suitelet Code: 

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */
define(['N/email', 'N/runtime', 'N/ui/serverWidget'], function(email, runtime, serverWidget) {
    function onRequest(context) {
        if (context.request.method === 'GET') {
            var form = serverWidget.createForm({
                title: 'Message Form'
            });

            var message = form.addField({
                id: 'message',
                type: serverWidget.FieldType.TEXTAREA,
                label: 'Message'
            });
            message.updateDisplaySize({
                height: 10,
                width : 60
            });

            form.addSubmitButton({
                label: 'Send Message'
            });
          
            form.addButton({
               label: "custom Button",
               id: "button1",
               functionName: "customFun"
            })
          
            form.clientScriptModulePath = 'SuiteScripts/testClientScript.js';

            context.response.writePage(form);
			
			
        }
        else
        {
            var message = context.request.parameters.message;
            context.response.write(message);
        }
    }

    return {
        onRequest: onRequest
    };
});







Contact Me

Name

Email *

Message *