Articles in this section

Example hook: Add a line item to a NetSuite order

This article demonstrates adding a line item to an order in NetSuite with JavaScript. Start by clicking the hook icon (​​hook.svg​​) on an import in Flow Builder.

360064400512-NetSuite_Import_Sales_Order.png

The following JavaScript function checks for empty data returned from an import step within a flow. Add the script to your account, 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
  }
 })
}