I would like to create an User Event script (Before Submit event), applied on Transactions (Purchase Orders, Sales Orders) that will Sort the Items lines based on a certain field (i.e. Item Name).
My approach would be to store all information in an array, sort it and then use the APIs nlapiRemoveLineItem method to remove each item and nlapiInsertLineItem method to insert each item on the correct order.
However, for this approach I would need to store all columns values before removing and inserting, and my concern is that NetSuite may raise an error due to any specific validation when inserting the line or when submitting the record.
Is there any different approach to make easier sorting the Line Items? Any suggestion would be highly appreciated.
Aside from performance concerns, there is an overriding issues that you cannot/must not "sort" lines once the line has a fulfillment or Invoice connected to it. So, you can only sort while the SO has nothing tied to it.
The reason is that you can't "move" line items - you'd have to erase it and re-add the item on a different line. Doing this will break hidden, inaccessible links NS maintains between the SO lines and subsequent transactions.
If you want to print a transaction report on created PO / SO, one common alternative is to sort the printed document only. Using nlapiXmlToPDF(), you create the PDF output in whatever order you want. This has some drawbacks as well, as the standard PDF and relating processes to rpint it can no longer be used, but this is better than sorting the actual lines.
One other approach is providing two sublists in the UI. The real NS sublist, which you don't sort, and a "fake" sublist maintained and sorted, using a widget that does allow sorting (perhaps going as far as a nifty Ext JS UI component, etc). The problem here is of course maintaining content synch between the two sublists, but as bad as that sounds in most cases it's STILL better than true sorting of the real sublist.
Reference : Scripted Sales Order Sorting: Suggestions