Change LIVE rate based on a select field

Jira Code: PROT-160

To change the ‘Live Rate’ minimum value to a new rate.

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
/**
 * Script Description
 * This script will update the SO Shipping cost based on the SHIPPING COST METHOD select field.
 */
/*******************************************************************************
 * ProTec
 * **************************************************************************
 * 
 * Date: 1-11-2018
 * 
 * Author: Jobin & Jismi IT Services LLP
 * 
 * 
 * REVISION HISTORY 
 * 
 * Revision 1 $ 1-11-2018 rosemol : Created
 * 
 *****************************************************************************
 **/
define(['N/currentRecord','N/record'],
		/**
		 * @param {record} record
		 */
function(currentRecord,record) {

	function pageInit(scriptContext) {    
	}
	
	function fieldChanged(scriptContext){
		try {	
			//Get the field change of SHIPPING COST METHOD field
			if (scriptContext.fieldId == 'custbody_prot139_shipping_cost_method'){			
				var soRecord = scriptContext.currentRecord;
				//get the field value of SHIPPING COST METHOD
				var sc_method = soRecord.getValue({                    
					fieldId : 'custbody_prot139_shipping_cost_method'
				});	
				if(sc_method == 4){ //if FREE SHIP is selected then set shipping cost as 0
				
					soRecord.setValue({
						fieldId  : 'shippingcost',
						value : 0
					});
					
				}
				else if(sc_method == 2){
					try {
						 //LIVE RATE is selected then set shipping cost as the 20% of subtotal
						
						var itemLineCount = soRecord.getLineCount({sublistId : 'item'}); //get itemlist count					
						var totalamount = 0;
						for(var i=0; i<itemLineCount; i++){
							var committed = soRecord.getSublistValue({ //get item committed count
		            		    sublistId: 'item',
		            		    fieldId: 'commitinventory',
		            		    line: i
		            		});
							
							if(committed == 1){
								var amount = soRecord.getSublistValue({  //get committed items amount
			            		    sublistId: 'item',
			            		    fieldId: 'amount',
			            		    line: i
			            		});
								totalamount = totalamount+amount;
							}
						}
							
						var liveRate = ((20*totalamount)/100).toFixed(2);				
						
						if (liveRate <= 15){
							soRecord.setValue({
								fieldId  : 'shippingcost',
								value : 15.00
							});
							
						}else{
							soRecord.setValue({
								fieldId  : 'shippingcost',
								value : liveRate
							});
							
						}					
					
					} catch (e) {
						logme('err@liverateShippingcost',getError(e));
					}
				}
				else{
					soRecord.setValue({
						fieldId  : 'shippingcost',
						value : 0
					});
					
				}
			}		
		
		} catch (e) {
			logme('err@fieldChanged',getError(e));
		}
	}
	return {
		pageInit: pageInit,
		fieldChanged: fieldChanged
	};

});
/*******************************************************************************
 * return error
 * 
 * @param e
 * @returns {String}
 */
function getError(e) {
	var stErrMsg = '';
	if (e.getDetails != undefined) {
		stErrMsg = '_' + e.getCode() + '<br>' + e.getDetails() + '<br>'
		+ e.getStackTrace();
	} else {
		stErrMsg = '_' + e.toString();
	}
	return stErrMsg;
}

/*******************************************************************************
 * Log these data
 */
function logme(title, details) {
	log.debug({
		title : title,
		details : details
	});
}

Leave a comment

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