Start a new topic

Working with "extra" data on order

We have some orders that will have a field called SHOP-TAGS that will be populated on some orders with the string: "EDIT ORDER"


We'd like to be able to search for these using using the SHOP-TAGS field and add a tag.


Could I get example code of how to search for the filed from the order's extra data and add a tag?


Thanks.


George,


I would recommend you set this up as follows:

  1. Create a Smart Filter on the Orders table, for status=Pending and the LOB in question here (since I know you are a 3PL with multiple LOB's).
  2. Create a Script (of Trigger type, Order record type, and beta version) to evaluate the extraneous data on an order - and if it finds the SHOP-TAGS="EDIT ORDER" value that you're looking for - then put a tag the order.  See below for example code. 
  3. Create a Trigger, using the Smart Filter and the Script from above.

Here's an example of a script that you could use - note, you may want to edit the Tag name being used (on line 23).


function getOrderExtraData(order, code)
{
   var extraDataList = order.extraOrderData;
   if(extraDataList != null)
   {
      for(var i=0; i<extraDataList.size(); i++)
      {
         var extraData = extraDataList.get(i);

         if(extraData.code == code)
         {
            return(extraData.value);
         }
      }
   }
   return(null);
}

var shopTags = getOrderExtraData(order, "SHOP-TAGS");
if (shopTags == "EDIT ORDER")
{
   utils.log("Tagging order as an EDIT ORDER");
   infoplusApi.addTag("Order", order.orderNo, "SHOPIFY-EDIT-ORDER");
}


Darin, 


That worked perfectly. I've been asked to modify this to take the second SHOP-TAG on these edited orders and populate the customerOrderNo, (or another a custom field). The second SHOP-TAG is always INFOPLUS-{original orderNo}. This will link the edited orders to the original. I've tried updating it, but I get a syntax error on line 38:


32 var originalOrder = getOrderExtraData(order, 'SHOP-TAGS')

33 if(originalOrder == null || originalOrder == "")

34 {

35 utils.log("Order [" + order.orderNo + "] does not have a value in SHOP-TAGS.");

36   return;

37 }

38 if(originalOrder !== null || !== "");

39 {

40 order.customFields.put(originalOrder, order.customerOrderNo);

41 infoplusApi.update("order", order);

42 utils.log("Order [" + order.orderNo + "] is edited order, adding original Infoplus order info");

43 }


Can you let me know what I'm overlooking that's keeping it from working?


Thanks.

George,


I believe to fix your syntax error, you'd need to make line 38:

    if(originalOrder !== null || originalOrder !== "");


Alternatively - I think to achieve what you want here, you could change line 38 to simply be an else - as in:


var originalOrder = getOrderExtraData(order, 'SHOP-TAGS')
if(originalOrder == null || originalOrder == "")
{
    utils.log("Order [" + order.orderNo + "] does not have a value in SHOP-TAGS.");
    return;
}
else
{
    order.customFields.put(originalOrder, order.customerOrderNo);
    infoplusApi.update("order", order);
    utils.log("Order [" + order.orderNo + "] is edited order, adding original Infoplus order info");
}


Finally, I think you're also going to run into issues on that order.customFields.put line.  This function (put) requires its first argument to be a name of a custom field on the Order table, and the second argument to be the value you want in that field.  I assume you'd want originalOrder to be your second argument (i.e., the value to store in the order), but the only custom field your account has on the orders table today is "temp1" - so you may try that for your first argument, ala:  order.customFields.put("temp1", originalOrder);.  


Alternatively, if you're trying to put this originalOrder value in the customerOrderNo field on the order, then you'd go with a simple:  order.customerOrderNo = originalOrder.  




Darin, 


I've got the script in place, but it's not working exactly as expected. Here's the script:


  

function getOrderExtraData(order, code)
{
   var extraDataList = order.extraOrderData;
   if(extraDataList != null)
   {
      for(var i=0; i<extraDataList.size(); i++)
      {
         var extraData = extraDataList.get(i);
         if(extraData.code == code)
         {
            return(extraData.value);
         }
      }
   }
   return(null);
}

var orderTag = getOrderExtraData(order, 'SHOP-SOURCE_NAME')
if(orderTag == null || orderTag == "")
{
   utils.log("Order [" + order.orderNo + "] does not have a value in SHOP-SOURCE_NAME.");
   return;
}

if(orderTag == "EDIT ORDER")
{
   utils.log("Order [" + order.orderNo + "] is edited order, applying tag to order");
   infoplusApi.addTag("order", order.orderNo, "631-EDITED-ORDER");
}

var originalOrder = getOrderExtraData(order, 'SHOP-TAGS')
if(originalOrder == null || originalOrder == "")
{
    utils.log("Order [" + order.orderNo + "] does not have a value in SHOP-TAGS.");
    return;
}
else
{
    //order.customFields.put(originalOrder, order.customerOrderNo);
    order.customerOrderNo = originalOrder
    infoplusApi.update("order", order);
    utils.log("Order [" + order.orderNo + "] is edited order, adding original Infoplus order info");
}

 

This is what's being logged on orders that don't meet any of the criteria evaluated in the script:


Order [4694991.000] is edited order, adding original Infoplus order info


I've compared the version I have in test with this one and can't find any differences, (the script in test performs as expected).


Could you take a look and let me know what I'm missing?


Thanks

Login or Signup to post a comment