Jackie
12/05/2023, 2:33 PMJackie
12/05/2023, 6:34 PM/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(['N/log', 'N/record'],
function (log, record) {
function beforeSubmit(context) {
try {
if (context.type === context.UserEventType.EDIT) {
// Get the customer record
var customerRecord = context.newRecord;
// Specify the subsidiary internal ID you want to remove
var subsidiaryToRemove = '3'; // Replace with the actual subsidiary internal ID
// Get the number of subsidiaries on the Subsidiaries sublist
var subsidiariesCount = customerRecord.getLineCount({
sublistId: 'subsidiary'
});
// Loop through each subsidiary on the Subsidiaries sublist
for (var line = subsidiariesCount - 1; line >= 0; line--) {
// Get the subsidiary internal ID from the sublist
var subsidiaryId = customerRecord.getSublistValue({
sublistId: 'subsidiary',
fieldId: 'subsidiary',
line: line
});
// If the current subsidiary matches the one to be removed, remove the line
if (subsidiaryId === subsidiaryToRemove && subsidiariesCount > 1) {
customerRecord.removeLine({
sublistId: 'subsidiary',
line: line
});
log.debug({
title: 'Subsidiary Removed',
details: 'Subsidiary ' + subsidiaryToRemove + ' removed from customer record.'
});
}
}
}
} catch (e) {
log.error({
title: 'Error',
details: e.toString()
});
}
}
return {
beforeSubmit: beforeSubmit
};
}
);
if anyone could point out what I am doing wrong or let me know of any problems with this id appreciate it. It ran prior as a onevent(VIEW), but it threw an error about not having permissions to edit, so I changed it to an on edit function, however now it does not seem to run at all.