My Sales Order has 2 lines. I am getting an unexpe...
# suitescript
h
My Sales Order has 2 lines. I am getting an unexpected error when removing the first line. But it is successful when I remove the second line. Anyone knows what's wrong?
Copy code
// Context Type: Create
function beforeLoad(scriptContext) {
  var invoice_record = scriptContext.newRecord;
  invoice_record.removeLine({
    sublistId: "item",
    line: 0
  });
}
Result: An unexpected error has occurred. Please click here to notify support and provide your contact information.
p
scriptContext.newRecord is read only
h
I believe it's not when on Create mode as I am able to set field values.
b
before load is for setting default values
anything that goes beyond that tends to fail
p
How are you trying to delete multiple lines? your example only shows removing line 0. I always delete lines from the one with the largest number to the smallest - in UI terms, from bottom to top. (that being said i agree with battk it might be not supported)
h
I am removing lines from 0 until the end. I have a checkbox that indicates the line items for removal. The script I pasted above is my simple test on the Invoice. I actually have a loop for the item sublist. I did try removing the last item line only
line: 1
and it worked. Seems like it's only having problems when removing item lines from the start.
p
Ok... loop from lineCount -1 to 0, instead of from 0 to lineCount -1.
p
Didn't notice the context. This is what I am using when clearing a sublist.
for (var nLine = recSalesOrder.getLineCount({sublistId: 'item'}) - 1; nLine >= 0; nLine--) {
recSalesOrder.removeLine({sublistId: 'item', line: nLine,  ignoreRecalc: true});
}
pretty much the same as what PabloZ suggested.
h
Thanks! I still get the error so I used pageInit in Client Script instead.