Requirement
When creating a sales order or open an existing sales order, before saving that SO we need to check whether the colorado delivery fee is added properly or not based on the shipping address as colorado.
Therefore, only shipping addresses to COLORADO are affected. Only customers who have to pay sales tax, must pay the delivery fee! Colorado delivery fee is as an item in the sales order.
Solution
In the script we will check the shipping address is “Colorado” and the customer who have to pay sales tax. If both condition satisfies then, we will consider as this order is eligible for adding Colorado delivery fee in the item line.
Colorado delivery fee is set as an item in the sales order. We will check whether the item colorado delivery fee is present in sales order. If it is not present, we will set that item in the sales order line level. The item colorado delivery fee is named as RTLDELFEE-CO (Other Charge for Sale)
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/record','N/url', 'N/https'],
(record,url,https) => {
const beforeSubmit = (scriptContext) => {
try {
var recObj = scriptContext.newRecord;
log.debug("recobj", recObj.id);
if (scriptContext.type == 'edit' || scriptContext.type == 'create' || scriptContext.type == 'copy') {
// var recObj = scriptContext.newRecord;
// log.debug("recobj", recObj.id);
var addrRecord = recObj.getSubrecord({
fieldId: 'shippingaddress'
});
log.debug("addrRecord", addrRecord);
var stateVal = addrRecord.getValue({
fieldId: 'state'
})
log.debug("stateVal", stateVal);
var entity = recObj.getValue({
fieldId: 'entity'
})
log.debug("entity", entity);
var taxTotal = recObj.getValue({
fieldId: 'taxtotal'
})
log.debug("taxTotal", taxTotal);
var line = recObj.getLineCount({
sublistId: 'item'
});
log.debug('lineCount in else condition', line);
if (stateVal != 'CO') {
log.debug("inside if beforesubmit condition", true);
log.debug('Both condition not satisfied,in else', 'Both condition not satisfied,in else');
for (var x = 0; x < line; x++) {
var coloradoItem = recObj.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line: x
});
log.debug('coloradoItem in else',coloradoItem);
if(coloradoItem == '1942') {
recObj.removeLine({
sublistId: 'item',
line: x,
});
}
}
}
}
}
catch(e){
log.debug('error@aftersubmit',e);
}
}
/**
* Defines the function definition that is executed after record is submitted.
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
* @since 2015.2
*/
const afterSubmit = (scriptContext) => {
try {
var recObj = scriptContext.newRecord;
log.debug("recobj", recObj.id);
if (scriptContext.type == 'edit' || scriptContext.type == 'create' || scriptContext.type == 'copy') {
// var recObj = scriptContext.newRecord;
// log.debug("recobj", recObj.id);
var addrRecord = recObj.getSubrecord({
fieldId: 'shippingaddress'
});
log.debug("addrRecord", addrRecord);
var stateVal = addrRecord.getValue({
fieldId: 'state'
})
log.debug("stateVal", stateVal);
var entity = recObj.getValue({
fieldId: 'entity'
})
log.debug("entity", entity);
var taxTotal = recObj.getValue({
fieldId: 'taxtotal'
})
log.debug("taxTotal", taxTotal);
if (stateVal == 'CO' && taxTotal > 0 && entity != 'CUST0190000 Nutrex Webshop') {
// && entity != '3967'
log.debug("inside if condition", true);
var suiteletURL = url.resolveScript({
scriptId: 'customscript_jj_sl_add_delivery_fee',
deploymentId: 'customdeploy_jj_sl_add_delivery_fee',
params: {
"id": recObj.id,
},
returnExternalUrl: true
});
log.debug('suiteletURL', suiteletURL);
var response = https.get({
url: suiteletURL,
});
log.debug('response', response);
}
}
}
catch(e){
log.debug('error@aftersubmit',e);
}
}
return {beforeSubmit,afterSubmit}
});
Suitelet
/**
* @NApiVersion 2.1
* @NScriptType Suitelet
*/
define(['N/record'],
(record) => {
/**
* Defines the Suitelet script trigger point.
* @param {Object} scriptContext
* @param {ServerRequest} scriptContext.request - Incoming request
* @param {ServerResponse} scriptContext.response - Suitelet response
* @since 2015.2
*/
const onRequest = (scriptContext) => {
var parameters = scriptContext.request.parameters;
log.debug('parameters', parameters);
var order = parameters.id;
var rec = record.load({
type: 'salesorder',
id: order,
isDynamic: true
});
var lineCount = rec.getLineCount({
sublistId: 'item'
});
log.debug('lineCount', lineCount);
var itemArr = [];
for (var i = 0; i < lineCount; i++) {
var itemColoradoFee = rec.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line: i
});
var taxAmount = rec.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_ava_taxamount',
line: i
});
itemArr.push(itemColoradoFee);
}
log.debug('itemArr', itemArr);
if(!itemArr.includes('1942')) {
rec.selectNewLine({
sublistId: 'item',
});
rec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value: '1942'
});
rec.commitLine({
sublistId: 'item'
});
var id = rec.save();
log.debug('SO id: ' + id + ' saved.');
}
else{
log.debug('inside else', 'inside else');
return;
}
}
return {onRequest}
});