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
  }
 })
}
Was this article helpful?
1 out of 2 found this helpful

Comments

4 comments
Date Votes
  • Hi,

    I wanted to add a hook but the flow from celigo "Magento Order to NetSuite Order Add" is locked and wont let you add a javascript except the ones originally added by celigo.

    How to go about using a hook with the flow?

    0
  • Hi Zaid Ghansar :) how are you?

    The flow in question is locked by design, as it's a prebuilt flow.

    If you want to make such modifications to a flow, the best option is to consider building your own flow. You could even start from a template flow (see this marketplace template) to get things going.

    That said, what's the use-case you're working on? What's the challenge you're trying to solve?

    Kind Regards,

    Adel

    0
  • I need to bring in our store credit (which is a discount on the magneto order) into netsuite as line item with negative values in addition to the current coupon code which is already brought in as a discount item.

    Issue is "discount item" is already in use in the template mapping so it wont let me add another "discount item" and "rate" says duplicate. If i can achieve that it will spare me from javascript

    0
  • Hi Zaid Ghansar

    Here's how I would consider doing that:

    1. Modify the mapping, in prebuilt flow, to change the status of the NS Sales Order to "PENDING APPROVAL", for example.
    2. Create a custom (scheduled) flow that reads the M2 orders and updates the NS Sales Order with the additional "store credit" item with negative values. Additionally, change the status of the NS SO to "PENDING FULFILMENT", or whichever status is appropriate.

    Kind Regards,

    Adel

    0

Please sign in to leave a comment.