Automate customer location in sales order.

Jira Code: ACL-2

Description

Sales order location is sourced from customer Primary location (Custom entity field)

Client Script

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/search'],
function(search){
function postSourcing(context){
try{
	if(context.fieldId=="entity"){//update location
	var customer=context.currentRecord.getValue({fieldId:"entity"});
    if(customer){var location=search.lookupFields({id:customer,type:"entity",columns:["custentity22"]}).custentity22[0].value;
    if(location)context.currentRecord.setValue({fieldId:"location",value:location});
                  }}	
     else if(context.fieldId=="item"&&context.sublistId=="item")//update item location
context.currentRecord.setCurrentSublistValue({sublistId:'item',fieldId:'location',value:context.currentRecord.getValue({fieldId:"location"}),ignoreFieldChange:true});    	
}
catch(err)
	{
console.log(err);
	}
}
function fieldChanged(context){
	try{
	if(context.fieldId=="location"&&context.sublistId!="item"){//on body line location change	
	//	console.log("TEST");
	var location=context.currentRecord.getValue({fieldId:"location"});
	var numLines = context.currentRecord.getLineCount({sublistId:'item'});
	//     try{
 //    context.currentRecord.setCurrentSublistValue({sublistId:'item',fieldId:'location',value:context.currentRecord.getValue({fieldId:"location"}),ignoreFieldChange:true});            
	// }catch(err){}
	for(var i=0;i<numLines&&location;i++){

	   if(context.currentRecord.getSublistValue({sublistId:'item',fieldId:'quantityfulfilled',line:i})==0)
	   	  {
       context.currentRecord.selectLine({sublistId:'item',line:i});
       context.currentRecord.setCurrentSublistValue({sublistId:'item',fieldId:'location',value:location,ignoreFieldChange:true}); 
       context.currentRecord.commitLine({sublistId:'item'});
       } 
                        }}
	}catch(err){console.log(err);}
}
return{postSourcing:postSourcing,fieldChanged:fieldChanged}
    });

Leave a comment

Your email address will not be published. Required fields are marked *