Jira Code: PROT-139
This client script will update the sales order shipping cost based on the SHIPPING COST METHOD select field. The script trigger when the SHIPPING COST METHOD field value is changed. Based on the field value it will set the shipping cost of the sales order.
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
console.log("free ship");
soRecord.setValue({
fieldId : 'shippingcost',
value : 0
});
}else if(sc_method == 2){ //LIVE RATE is selected then set shipping cost as the 20% of subtotal
var itemLineCount = soRecord.getLineCount({sublistId : 'item'});
console.log('itemLineCount');
var totalamount = 0;
for(var i=0; i<itemLineCount; i++){
var committed = soRecord.getSublistValue({
sublistId: 'item',
fieldId: 'commitinventory',
line: i
});
if(committed == 1){
var amount = soRecord.getSublistValue({
sublistId: 'item',
fieldId: 'amount',
line: i
});
totalamount = totalamount+amount;
}
}
console.log('totalamount',totalamount);
var liveRate = ((20*totalamount)/100).toFixed(2);
console.log('liveRate',liveRate);
if (liveRate <= 12){
soRecord.setValue({
fieldId : 'shippingcost',
value : 12.00
});
}else{
soRecord.setValue({
fieldId : 'shippingcost',
value : liveRate
});
}
}else{
soRecord.setValue({
fieldId : 'shippingcost',
value : 0
});
console.log('No change for shipping cost');
}
}
} 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
});
}