When an item is added using a scanner, an alert message needs to display when the quantity added in the item fulfillment record is greater than the maximum.
Create a client script to catch the field trigger of ‘Select Item’ field when the item is added using a scanner. The total quantity both before and after the scan will be compared while adding a new item. If there are no changes in the total quantity, then we can assume that the currently added item has reached its maximum fulfillable quantity. At this point, an alert message will be displayed.
define([‘N/currentRecord’, ‘N/record’, ‘N/search’, ‘N/ui/dialog’],
/**
* @param{currentRecord} currentRecord
* @param{record} record
* @param{search} search
*/
function (currentRecord, record, search, dialog) {
var temp = 0;
const main = {
/**
* Function to be executed after page is initialized.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord – Current form record
* @param {string} scriptContext.mode – The mode in which the record is being accessed (create, copy, or edit)
*
* @since 2015.2
*/
pageInit: function (scriptContext) {
try {
const WINDOW_ALERT = window.alert;
window.alert = function () {
console.log(‘Triggered’, arguments);
if (arguments && arguments[0] == ‘No Match.’) {
dialog.alert({
title: ‘Invalid Entry!’,
message: ‘Invalid Item selected. Please try with another item.’
}).then(success).catch(failure);
function success(result) {
}
function failure(reason) {
console.log(“failure”, failure)
}
} else {
WINDOW_ALERT.apply(this, arguments)
}
}
} catch (e) {
console.log(‘err@pageInit’, e)
}
},
/**
* Function to be executed when field is changed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord – Current form record
* @param {string} scriptContext.sublistId – Sublist name
* @param {string} scriptContext.fieldId – Field name
* @param {number} scriptContext.lineNum – Line number. Will be undefined if not a sublist or matrix field
* @param {number} scriptContext.columnNum – Line number. Will be undefined if not a matrix field
*
* @since 2015.2
*/
fieldChanged: function (scriptContext) {
try {
var scannedQuantityCount = 0; // inital count before the scanning process
var currentRecord = scriptContext.currentRecord;
var triggerField = scriptContext.fieldId;
var triggerSublistField = scriptContext.sublistId
if (triggerField == ‘quantity’) { // To check the Quantity count
var lineCount = currentRecord.getLineCount({sublistId: ‘item’})
for (var i = 0; i < lineCount; i++) {
scannedQuantityCount = scannedQuantityCount + currentRecord.getSublistValue({
sublistId: “item”,
fieldId: “quantity”,
line: i
});
}
if (temp != scannedQuantityCount) { // when a new quantity is added using the scanner, then the ‘temp’ value will change else it remains the same.
temp = scannedQuantityCount;
} else { // When there are no changes in the temp value, then it means that the quantity will reach its maximum limit. At this time an alert will display.
dialog.alert({
title: ‘Invalid Entry!’,
message: ‘Item quantity exceeded. Please try with another item.’
}).then(success).catch(failure);
function success(result) {
}
function failure(reason) {
console.log(“failure”, failure)
}
}
}
} catch (e) {
console.log(‘err@fieldChanged’, e)
}
},
}
return main;
});