steven.smith
03/17/2023, 1:16 PMLucas
03/17/2023, 5:09 PM/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
define(['N/record'], function(record) {
function beforeLoad(context) {
if (context.type === context.UserEventType.VIEW || context.type === context.UserEventType.EDIT) {
var itemFulfillment = context.newRecord;
// Update the location field value
var newLocationId = '12345'; // Replace with the desired location internal ID
itemFulfillment.setValue({
fieldId: 'location',
value: newLocationId
});
}
}
return {
beforeLoad: beforeLoad
};
});
2. Create a Client Script to update the UI and handle inventory detail configuration:
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define([], function() {
function pageInit(context) {
var itemFulfillment = context.currentRecord;
// Update the location in the UI
var newLocationId = '12345'; // Replace with the desired location internal ID
itemFulfillment.setValue({
fieldId: 'location',
value: newLocationId
});
// Handle inventory detail
var itemSublistId = 'item';
var itemCount = itemFulfillment.getLineCount({ sublistId: itemSublistId });
for (var i = 0; i < itemCount; i++) {
// Create inventory detail subrecord
itemFulfillment.selectLine({ sublistId: itemSublistId, line: i });
var inventoryDetailSubrecord = itemFulfillment.getCurrentSublistSubrecord({
sublistId: itemSublistId,
fieldId: 'inventorydetail'
});
if (!inventoryDetailSubrecord) {
inventoryDetailSubrecord = itemFulfillment.createCurrentSublistSubrecord({
sublistId: itemSublistId,
fieldId: 'inventorydetail'
});
}
// Set inventory detail subrecord fields
var quantity = itemFulfillment.getCurrentSublistValue({ sublistId: itemSublistId, fieldId: 'quantity' });
inventoryDetailSubrecord.setValue({ fieldId: 'quantity', value: quantity });
// Add or update inventory assignment
var inventoryAssignmentCount = inventoryDetailSubrecord.getLineCount({ sublistId: 'inventoryassignment' });
if (inventoryAssignmentCount === 0) {
inventoryDetailSubrecord.selectNewLine({ sublistId: 'inventoryassignment' });
} else {
inventoryDetailSubrecord.selectLine({ sublistId: 'inventoryassignment', line: 0 });
}
// Set inventory assignment fields
inventoryDetailSubrecord.setCurrentSublistValue({ sublistId: 'inventoryassignment', fieldId: 'quantity', value: quantity });
inventoryDetailSubrecord.setCurrentSublistValue({ sublistId: 'inventoryassignment', fieldId: 'location', value: newLocationId });
// Commit the inventory assignment line
inventoryDetailSubrecord.commitLine({ sublistId: 'inventoryassignment' });
// Save the inventory detail subrecord
itemFulfillment.commitLine({ sublistId: itemSublistId });
}
}
return {
pageInit: pageInit
};
});
This code snippet assumes that the new location does not use bins. It creates or updates the inventory detail subrecord for each item in the item fulfillment and sets the inventory assignment with the new location and the item's quantity.
Please note that this example assumes a simple scenario and might need to be adjusted to fit your specific business logic and requirements.reptar
03/17/2023, 10:29 PM