Here are some real-world examples of solutions that have been built in Infoplus using Scripts.  For more examples see the articles written under our Scripts bridge page


Automatically place Orders on Hold

Use Case:  You want to place a Hold on any Order that is placed meeting certain conditions (such as, for a total value over some amount, or for a specific line item, or with a special ship-to destination).  


This script could be assigned to run with a Trigger that searched for new Orders containing the Item "SOURCE".

order.holdCode = "H";
 
// explicitly clear these values, to avoid "not modifyable" error
order.extraLineItemData = null;
order.extraOrderData = null;
 
utils.log("Will try to set order " + order.orderNo + " hold code to H...");
infoplusApi.update("order", order);
utils.log("Set order " + order.orderNo + " hold code to H");


Add a Line Item to an Order

Use Case:  You want to a certain line item to any order that matches a specific condition.  This script could be assigned to run with a Trigger that identified orders with the condition in question.  

var newLineItem = infoplusApi.constructModel("orderLine");
newLineItem.sku = "MY-SKU"
newLineItem.orderedQty = 1;
order.addToLineItems(newLineItem);
infoplusApi.update("order", order);


Update the names of all Third Party Parcel Accounts to match their Account Numbers

Use Case:  To clean up the data in your Third Party Parcel Account table, you want to update all records so that their Account Names match their Account Numbers.  


This can be done by creating a simple script like this, then selecting rows from the Third Party Parcel Accounts table and using the Bulk Run Script process.

thirdPartyParcelAccount.accountName = thirdPartyParcelAccount.accountNo;
utils.log("Updating thirdPartyParcelAccount [" + thirdPartyParcelAccount.id + "] to have name [" + thirdPartyParcelAccount.accountName + "].");
infoplusApi.update("thirdPartyParcelAccount", thirdPartyParcelAccount);


Split an Order into Two Orders

Use Case:  Any time an Order is placed for any one or more specific SKUs, peel those SKUs off into a second order (with the same root order number).  


To use this script, you'll need to replace the top lines with the SKUs you want to be peeled off (or customize the if statement on line 23 to use your own custom rule).


This script could be assigned to run with a Trigger that searched for new Orders containing the Item "SOURCE".

///////////////////////////////////////////////////////////
// define all SKUs we want to strip off into a new order //
///////////////////////////////////////////////////////////
var skusToStrip = {};
skusToStrip["BASIC6"] = true;
skusToStrip["BASIC7"] = true;
skusToStrip["BASIC8"] = true;
skusToStrip["BASIC9"] = true;
skusToStrip["BASIC10"] = true;

//////////////////////////////////////////////////////////////////////
// look at line items on the order - if any are for one of the SKUs //
// we want to strip off - then collect those in a list.  Also keep  //
// a list of the line items to Keep on the original order           //
//////////////////////////////////////////////////////////////////////
var lineItemsToStrip = [];
var lineItemsToKeep = [];
if(order.lineItems)
{
   for(var i=0; i<order.lineItems.size(); i++)
   {
      var lineItem = order.lineItems.get(i);
      if(skusToStrip[lineItem.sku])
      {
         lineItemsToStrip.push(lineItem);
      }
      else
      {
         lineItemsToKeep.push(lineItem);
      }
   }
}

////////////////////////////////////////////////////////
// if none of the "strip" line items were found, exit //
////////////////////////////////////////////////////////
if(lineItemsToStrip.length == 0)
{
   utils.log("Did not find any skus to strip on order.  Exiting.");
   return;
}

//////////////////////////////////////////////////////
// if no line items were found to "keep", then exit //
//////////////////////////////////////////////////////
if(lineItemsToKeep.length == 0)
{
   utils.log("Did not find any skus to keep on order.  Exiting.");
   return;
}

////////////////////////////////////////////////////////////////////////////
// build a new order, a copy of the original, with the "strip" line items //
////////////////////////////////////////////////////////////////////////////
var blankOrder = infoplusApi.constructModel("order");
var newOrder = infoplusApi.duplicate("order", order.orderNo);
newOrder.lineItems = blankOrder.lineItems;
newOrder.useOrderNoRoot = Math.floor(order.orderNo);

for(var i=0; i<lineItemsToStrip.length; i++)
{
    var sourceLineItem = lineItemsToStrip[i];
    var newLineItem = infoplusApi.duplicate("orderLine", sourceLineItem.id);
    newLineItem.orderedQty = sourceLineItem.orderedQty;
    newLineItem.sku = sourceLineItem.sku;
    newOrder.addToLineItems(newLineItem);
}

///////////////////////
// add the new order //
///////////////////////
var addedOrder = infoplusApi.add("order", newOrder);
utils.log("Created new order [" + addedOrder.orderNo + "].");

////////////////////////////////////////////////////////////////////////////////////////
// re-build the line items array on the original order, to only have the "keep" lines //
////////////////////////////////////////////////////////////////////////////////////////
var blankOrder = infoplusApi.constructModel("order");
order.lineItems = blankOrder.lineItems;

for(var i=0; i<lineItemsToKeep.length; i++)
{
    var sourceLineItem = lineItemsToKeep[i];
    var newLineItem = infoplusApi.duplicate("orderLine", sourceLineItem.id);
    newLineItem.orderedQty = sourceLineItem.orderedQty;
    newLineItem.sku = sourceLineItem.sku;
    order.addToLineItems(newLineItem);
}

infoplusApi.update("order", order);
utils.log("Updated source order [" + addedOrder.orderNo + "].");


Peel a special SKU off into a new order

Use Case:  Any time an Order is placed for the Item "SOURCE", a second order should be created, with the same root order number, for the Item "TARGET".


This script could be assigned to run with a Trigger that searched for new Orders containing the Item "SOURCE".

var SOURCE_SKU = "SOURCE";
var TARGET_SKU = "TARGET";

var sourceLineItem = null;
if(order.lineItems)
{
   for(var i=0; i<order.lineItems.size(); i++)
   {
      if(order.lineItems.get(i).sku == SOURCE_SKU)
      {
         sourceLineItem = order.lineItems.get(i);
         break;
      }
   }
}

if(sourceLineItem == null)
{
   utils.log("Did not find source sku [" + SOURCE_SKU + "] on order.  Exiting.");
   return;
}

var blankOrder = infoplusApi.constructModel("order");
var newOrder = infoplusApi.duplicate("order", order.orderNo);
newOrder.lineItems = blankOrder.lineItems;

var newLineItem = infoplusApi.duplicate("orderLine", sourceLineItem.id);
newLineItem.sku = TARGET_SKU;
newLineItem.orderedQty = sourceLineItem.orderedQty;

newOrder.addToLineItems(newLineItem);
newOrder.useOrderNoRoot = Math.floor(order.orderNo);
newOrder.holdCode = null;
newOrder.carrierId = 0;
newOrder.needByDate = null;
newOrder.deliverOnDate = null;
newOrder.shipVia = null;

var addedOrder = infoplusApi.add("order", newOrder);
utils.log("Created order [" + addedOrder.orderNo + "] with SKU [" + TARGET_SKU + "].");


Change an Order containing Items set as Hazmat to land on hold with a Tag

Use Case: When an Order lands for an Item set up as Hazmat, place the Order on hold and add a tag to it.

if(order.lineItems)
{
   var targetHoldCode = "H";
   if(order.holdCode.equals(targetHoldCode))
   {
      utils.log("Order already has holdCode " + targetHoldCode);
   }
   else
   {
      ///////////////////////////////////////////////////
      // setup a filter for all the items on the order //
      ///////////////////////////////////////////////////
      var itemFilter = "lobId eq " + order.lobId + " and sku in (";

      for(var i = 0; i < order.lineItems.size(); i++)
      {
         var lineItem = order.lineItems.get(i);
         itemFilter += (i > 0 ? "," : "") + "'" + lineItem.sku + "'";
      }
      itemFilter += ")";

      var orderContainsHazmat = false;
      var items = infoplusApi.search("item", itemFilter, null, 100, null);

      if(items.size() > 0)
      {
         /////////////////////////////////////////////////////////
         // iterate over item list looking for any hazmat items //
         /////////////////////////////////////////////////////////
         for(var i = 0; i < items.size(); i++)
         {
            var item = items.get(i);
            if(item.hazmat.equals("Yes"))
            {
               utils.log("Found Hazmat sku " + item.sku + " or order");
               orderContainsHazmat = true;
               break;
            }
         }
      }
      else
      {
         utils.log("No items found from order line items");
      }

      if(orderContainsHazmat)
      {
         ////////////////////////////////////////////////////////////////////
         // explicitly clear these values, to avoid "not modifyable" error //
         ////////////////////////////////////////////////////////////////////
         order.extraLineItemData = null;
         order.extraOrderData = null;

         ////////////////////////////
         // add a tag to the order //
         ////////////////////////////
         infoplusApi.addTag("order", order.orderNo, "hazmat-shipment");

         //////////////////////////////////
         // put a hold code on the order //
         //////////////////////////////////
         order.holdCode = targetHoldCode;

         utils.log("setting order hold on " + order.orderNo + " found hazmat items");
         infoplusApi.update("order", order);
      }
   }
}