Scenario:
In the sales orders, we can enter any number of item line quantities. But from the client business perspective, they need to control the quantity based on the item case pack. The quantity should be multiples of the case pack number and if the quantity is not a case pack multiple we need to correct the value. Also need to add a new column field “Override” and this box is checked, we can bypass the process and keep the quantity value.
EG: CASE pack – 4
Line Quantity should – 4/8/12/16…
Solution:
We can do this by using a client script validation on Sales Order.
//JJ CS validateCasepackQuantity.js
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/currentRecord', 'N/record', 'N/search'],
/**
* @param{currentRecord} currentRecord
* @param{record} record
* @param{search} search
*/
function(currentRecord, record, search) {
/**
* Validation function to be executed when sublist line is committed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
*
* @returns {boolean} Return true if sublist line is valid
*
* @since 2015.2
*/
function validateLine(scriptContext) {
try{
var currentRecord = scriptContext.currentRecord
var itemRecId = currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'item'
})
var itemtype = scriptContext.currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'itemtype'
})
var checkvalue = scriptContext.currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_override_quantity'
})
if(checkvalue==false) {
if (itemtype == 'InvtPart') {
var fieldLookUp = search.lookupFields({
type: record.Type.INVENTORY_ITEM,
id: itemRecId,
columns: ['custitem4']
});
var casePack = fieldLookUp.custitem4
var qty = currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity'
})
if(Number(qty) == 0){
alert("Quantity Should be Greater than zero")
return false;
}
if(Number(casePack) == 0){
alert("casePack Should be Greater than zero")
return false;
}
if (Number(qty) % Number(casePack) != 0) {
if (qty < casePack) {
alert("Quantity is less than casePack. Quantity should be equal or multiples of Case Pack")
currentRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: Number(casePack)
})
}
if (qty > casePack) {
alert("Quantity is greater than casePack. Quantity should be equal or multiples of Case Pack")
var updatedQty = Number(casePack) * (Math.ceil(Number(qty) / Number(casePack)))
currentRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: updatedQty
})
}
}
else {
if(qty==casePack){
currentRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: Number(qty)
})
}
else {
alert("Quantity is greater than casePack. Quantity should be equal or multiples of Case Pack")
var updatedQty = Number(casePack) * (Math.ceil(Number(qty) / Number(casePack)))
currentRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: updatedQty
})
}
}
return true
}
}
else {
return true
}
}
catch (e) {
console.log("Error @ validateLine: ",e.name+" : "+e.message)
}
}
return {
validateLine: validateLine
};
});


