I'm working on a project through which users will be using a modified inventory adjustment form to perform an inventory "checkout" process. In other words, I'm setting up a form where the quantity on the adjustment line will always be entered as a negative. Most items are in a single bin. I'd like to have a client script that auto-fills the inventory detail after the adjustment quantity is entered on the line. The logic it would follow is essentially this: 1) If the item is only in one bin, use that bin for the full negative quantity; 2) if the item is in multiple bins AND the preferred bin has the full required quantity, use the preferred bin; 3) In any other situation, do nothing - the user must do the inventory detail manually.
Below is a script that ChatGPT produced for this functionality, which doesn't work. I'm curious to hear from the experts here if this kind of thing sounds feasible and what you would suggest I do to get the result I need.
Thank you!
example script:
/**
*@NApiVersion 2.1
*@NScriptType ClientScript
*/
define(['N/search', 'N/record'], function(search, record) {
function postSourcing(context) {
var currentRecord = context.currentRecord;
var sublistName = context.sublistId;
var fieldId = context.fieldId;
var lineNum = context.line;
var quantity = currentRecord.getCurrentSublistValue({
sublistId: sublistName,
fieldId: 'adjustqtyby',
line: lineNum
});
if (fieldId === 'adjustqtyby') {
var inventoryDetailSublist = currentRecord.getSublistSubrecord({
sublistId: sublistName,
fieldId: 'inventorydetail',
line: lineNum
});
var inventoryAssignmentSublist = inventoryDetailSublist.getSublist({
sublistId: 'inventoryassignment'
});
var binCount = inventoryAssignmentSublist.getLineCount({
sublistId: 'inventoryassignment'
});
if (binCount === 1) {
inventoryAssignmentSublist.selectLine({
sublistId: 'inventoryassignment',
line: 0
});
inventoryAssignmentSublist.setCurrentSublistValue({
sublistId: 'inventoryassignment',
fieldId: 'quantity',
value: quantity
});
inventoryAssignmentSublist.commitLine({
sublistId: 'inventoryassignment'
});
} else if (binCount > 1) {
var preferredBin = inventoryAssignmentSublist.findSublistLineWithValue({
sublistId: 'inventoryassignment',
fieldId: 'preferredbin',
value: true
});
if (preferredBin !== null && inventoryAssignmentSublist.getSublistValue({
sublistId: 'inventoryassignment',
fieldId: 'quantity',
line: preferredBin
}) >= quantity) {
inventoryAssignmentSublist.selectLine({
sublistId: 'inventoryassignment',
line: preferredBin
});
inventoryAssignmentSublist.setCurrentSublistValue({
sublistId: 'inventoryassignment',
fieldId: 'quantity',
value: quantity
});
inventoryAssignmentSublist.commitLine({
sublistId: 'inventoryassignment'
});
}
}
}
}
return {
postSourcing: postSourcing
};
});