Friday, May 17, 2019

SuiteScript: Set the Shipping Method based on the Shipping State selected on Sales Order

Scenario: When creating Sales Orders automatically set the Shipping Method To FedEx 2 Day when the Shipping State selected is CA or WA.
For all the other states the Shipping Method should be set to FedEx Ground.

Desired result can be achieved through a Client Script that will execute on Ship To Field Changed:

  1. Create script file with Field Changed function that will execute whenever a value is selected in Ship To field. The script can be similar to the one below:
    function retrieveShippingAddressState(type, name){	if (name == 'shipaddresslist')        { 	//get the value of the selected address	var shipAdddresSelected = nlapiGetFieldValue('shipaddresslist');	//get the customer	var customerId = nlapiGetFieldValue('entity');            	//define the search to retrive customer address with state details	var filters = new Array();	filters[0] = new nlobjSearchFilter( 'internalid', null, 'is', customerId);	filters[1] = new nlobjSearchFilter( 'isinactive', null, 'is', 'F');        	var arrSearchColumns = new Array();	arrSearchColumns[0] = new nlobjSearchColumn('address'); 	arrSearchColumns[1] = new nlobjSearchColumn('addressinternalid'); 	arrSearchColumns[2] = new nlobjSearchColumn('state');                 	var searchResults = nlapiSearchRecord('customer', null,filters, arrSearchColumns);	for ( var i = 0; searchResults != null && i < searchResults.length; i++ ) 	{		//parse the results of the search		var addressDetails = searchResults[i];		var addressID = addressDetails.getValue('addressinternalid');             		if (addressID == shipAdddresSelected)		{ 			//if the selected address has been found in the search results			//and state is one of CA, OR, WA, set the Ship Method to  FedEx 2 Day			var stateSelected  = addressDetails.getValue('state');						if (stateSelected == 'CA'|| stateSelected == 'WA')				nlapiSetFieldValue('shipmethod', 598);		else			//otherwise Ship Method is FedEx Ground			nlapiSetFieldValue('shipmethod', 596); 		}	}	}} 
  2. Create the Client Script record in NetSuite through Customization > Scripting > Scripts > New > Client Script.
    - Scripts tab: select the Script File = the name of the script you have uploaded in step 1 and in Field Changed Function = retrieveShippingAddressState
    - Deployments tab: Applies To = Sales Order, Status = Released, Select Audience

For more information about Client Scripts please visit SuiteAnswers Article 19194 Running Scripts in NetSuite Overview .

No comments:

Post a Comment