Tuesday, April 16, 2019

SuiteScript >Remove Sublist Line Items on a Record Starting from First Line Item to the Last One to Prevent this Error : You cannot delete the end of the group line. You need to delete the group' when item groups exist on a transaction

SuiteAnswers article id 19378-'Remove all sublist line items in a record using SuiteScript ' allows users to delete line items starting from the last line item. This works fine for transactions that do not have item groups with 'Reference Start/End Lines on picking tickets' checkbox enabled. For Items groups with 'Reference Start/End Lines on picking tickets' checkbox enabled, adding an item group to a transaction will add two extra line items: 'Item Group Name' and 'End of Group'. Using SuiteScript to delete sublist items on this transaction starting from the last line item will throw  the error  'You cannot delete the end of the group line. You need to delete the group'. To prevent this error, user needs to delete the item group itself which can only be done if line items on the transaction are removed starting from the first line item.

To remove the items on this transaction starting from the first line item using the following sample script:

var opportunityRecord = nlapiLoadRecord('opportunity', 285); // Load the transaction record
var oppItemCount = opportunityRecord.getLineItemCount('item'); // get the number of line items on the transaction

for(var i = 1; i < oppItemCount; i++)  // loop through all the line items
{
       //Do the logic here. Remove or Update Line item.
       var x=1;
       opportunityRecord.removeLineItem('item', x); // remove the first line item

 }
nlapiSubmitRecord(opportunityRecord, true, true); // submit the record after all line items have been deleted.

Note: In this script, we are always deleting the first line item, because as soon as it deletes the first line item on the transaction, the second line item becomes the first line item.


DISCLAIMER: The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. NetSuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations. NetSuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. NetSuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto. NetSuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.

No comments:

Post a Comment