ANS-1462 · SUITESCRIPT DEVELOPMENT
How to Correctly Remove All Line Items from a NetSuite Sublist Using SuiteScript 2.x
Understand the common pitfalls of iterating through and deleting sublist entries and implement an efficient SuiteScript 2.x solution.
Short answer
To reliably remove all line items from a NetSuite sublist using SuiteScript 2.x, avoid iterating forward. Instead, use a `while` loop that continuously removes the first line item (index 0) until the sublist is empty. This method prevents indexing errors caused by sublist re-indexing after each removal.
Scenario
Developers often encounter challenges when attempting to clear all line items from a NetSuite sublist programmatically. A common intuitive approach involves iterating through the sublist and removing items by their index. However, this method frequently leads to incomplete deletions because the sublist re-indexes itself after each item is removed, causing subsequent iterations to skip elements.
Solution
Observe the two code snippets below, demonstrating an incorrect and a correct approach to removing sublist items in SuiteScript 2.x. The key difference lies in how the removal operation handles the dynamic re-indexing of sublist lines.
INCORRECT APPROACH
javascript
for(var i = 0; i < currentRecord.getLineCount({ sublistId: 'item' }); i++){
currentRecord.removeLine({ sublistId: 'item', line: i });
}In the above snippet, which represents a common intuitive approach, the issue arises because once a line item is removed, the sublist re-indexes. This adjustment means that the line count and the indices of remaining items change. For example, if a sublist has three items (indexed 0, 1, 2):
LINE 1 [index 0] LINE 2 [index 1] LINE 3 [index 2]
currentRecord.removeLine({ sublistId: 'item', line: 0 })removes LINE1.
Result: LINE 2 [index 0] LINE 3 [index 1]
The loop increments
ito 1.currentRecord.removeLine({ sublistId: 'item', line: 1 })removes LINE 3 (which was originally LINE 3, now at index 1).Result: LINE 2 [index 0]
The loop then terminates as
iis now greater than the line count, leaving LINE 2 unremoved.CORRECT APPROACH
A straightforward and reliable method to address this is to consistently remove the first element (index 0) until no items remain in the sublist. This approach works because removing the first item always shifts the subsequent items to fill the gap, making the 'new' first item always available at index 0.
javascript
while(currentRecord.getLineCount({ sublistId: 'item' }) > 0){
currentRecord.removeLine({ sublistId: 'item', line: 0 });
}This will result in the following example execution:
Consider the same initial sublist:
LINE 1 [index 0] LINE 2 [index 1] LINE 3 [index 2]
currentRecord.removeLine({ sublistId: 'item', line: 0 })removes LINE1.
Result: LINE 2 [index 0] LINE 3 [index 1]
currentRecord.removeLine({ sublistId: 'item', line: 0 })removes LINE2.
Result: LINE 3 [index 0]
currentRecord.removeLine({ sublistId: 'item', line: 0 })removes LINE3.
Result: (Empty Sublist)
This ensures all items are correctly removed without any indexing complications.
Expert NetSuite Support
Need help with this NetSuite issue?
SuiteScript Development consulting and configuration support
