Common mistake in removing/clearing sub-list line items:
function removeOpportunityClearItems() {
var opportunityRecord = nlapiLoadRecord('opportunity', 1);
var oppItemCount = opportunityRecord.getLineItemCount('item');
for(var i = 1; i <= oppItemCount; i++) {
//Do the logic here. Remove or Update Line item.
opportunityRecord.removeLineItem('item', i);
}
nlapiSubmitRecord(opportunityRecord, true, true);
}
The code above would work when updating one or more fields on the sub-list line items. However, when removing line items, the code above would generate an error if the item sublist has more than one line item. The error that would say, "SSS_INVALID_SUBLIST_OPERATION: You have attempted an invalid sublist or line item operation. You are either trying to cannot access a field on a non-existent line or you are trying to add or remove lines from a static sublist."
Below is an explanation:
1. The Opportunity Record has two line items for example.
2. On the first iteration of the for loop, the variable i will have a value of 1 thus, it will remove the first line item.
3. On the second iteration of the for loop, the variable i will have a value of 2 thus, it will remove the second line item which no longer exists since the second line item is now the first.
4. The code will then throw the error stated above.
To remove all the line items in a record, one should start the removal process from the last line item. A sample code is given below:
function removeOpportunityLineItems() {
var opportunityRecord = nlapiLoadRecord('opportunity', 1);
var oppItemCount = opportunityRecord.getLineItemCount('item');
for(var i = oppItemCount; i >= 1; i--) {
opportunityRecord.removeLineItem('item', i);
}
nlapiSubmitRecord(opportunityRecord, true, true);
}
The code above will remove the last line item first up to 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