Adding a line item to a sales order in NetSuite using the hook icon on an import flow.

The following JavaScript function checks for empty data returned from an import flow. Add the script and then apply this function to an import hook.
function addDiscountLineItem(options) {
return options.data.map((d) => { // for each order
var errorsArray = [];
var errorMessage = '';
const error = {};
if(d === null || d.length === 0) {
errorMessage = 'd is NULL or EMPTY';
error = {code:"ERROR", message: errorMessage, source:'addDiscountLineItem'};
errorsArray.push(error);
console.log(error);
}
var items = d.order.items || [];
var lineItem;
var discountAmount = 0;
var diff = 0;
try {
for(var i=0; i < items.length; i++) { // for each item in order
lineItem = items[i];
diff = parseFloat(lineItem.gross) - parseFloat(lineItem.base);
if (diff != 0) {
discountAmount += diff;
}
}
if (discountAmount != 0) {
var discountLineItem = {
"number": "0",
"quantity": "1",
"gross": discountAmount,
"base": discountAmount
};
items.push(discountLineItem);
d.order.items = items;
}
}
catch(err) {
errorsArray.push(err);
}
return {
data: d
}
})
}