RE: Transform sales order to Return Authorization with specific items – SuiteScript 2.0

I need to transform a sales order to return authorization with specific line items, not will all line items available on the sales order. Let’s say I have a sales order created with 3 items

  • Item one with qty 2
  • Item two with qty 1
  • Item three with qty 4

this order fulfilled completely and billed. Now, the customer wanted to return Item two with qty 1 and Item three with qty 2, I want to transform (using suite script 2.0) my sales order to return authorization with only return items, I don’t want to include items that are not returned.

Returned items are available in an array;

const returned_items = [{ item: 'item two', qty: 1 }, { item: 'item three', qty: 2 }];
var returnAuthorizationRecord = record.transform({ 
fromType: record.Type.SALES_ORDER, 
fromId: orderId, 
toType: record.Type.RETURN_AUTHORIZATION,
isDynamic: true,
defaultValues: { 
customform: CUSTOM_FORMS_RMA.CREDIT 
} 
});
const lineCount = returnAuthorizationRecord.getLineCount({ sublistId: 'item' }); 
for (var i = 0; i < lineCount; i++) { 
returnAuthorizationRecord.selectLine({ sublistId: 'item', line: i });
// Do I need to first remove all line items and then add retuned items available in array 
returnAuthorizationRecord.removeLine({ sublistId: 'item', line: 3, ignoreRecalc: true });
// Or loop through each line and match with item, if item matches then update accordingly if not then remove from the list? }
returnAuthorizationRecord.save();

Add Comment
2 Answers

Removing all the line items isnt an option if you want to maintain the link between the sales order and the return authorization items.

Loop through the lines to do your matching and then remove your unmatched lines.

If you have to write more robust code that handles edge cases like duplicate items, then I recommend starting with all lines at 0 quantity, do your matching (potentially over several lines) and then remove lines with 0 quantity.

Advanced Answered on May 6, 2021.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.