I have created a pageInit SuiteScript that should remove line items on a Sales Order if they have a custom field filled out. The issue I am having is that it will not remove the line if it is the last line and no other line has a value for loss.
function pageInit(context) {
var objRec = context.currentRecord;
var itemsLength = objRec.getLineCount({
sublistId: 'item'
});
for (var i = itemsLength-1; i >= 0; i--){
var loss = objRec.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_linelossreason',
line: i
});
if (loss) {
objRec.removeLine({
sublistId: 'item',
line: i,
ignoreRecalc: true
});
log.debug('removed', 'Line ' + i + ' has been removed.');
} else {
log.debug('no removal', 'Line ' + i + ' will remain.');
}
}