Handlebar Syntax Help

Given this resource:

{
"data": [
[
{
"id": "6402565",
"recordType": "itemfulfillment",
"TrackingNumber": "593470228420",
"Ship Via": "FedEx Ground",
"ItemName": "WP36-HON-3P",
"Quantity": "1",
"Document Number": "IF120193",
"SellBrite Order Number": "13999",
"Carrier": "Standard"
},
{
"id": "6402565",
"recordType": "itemfulfillment",
"TrackingNumber": "593470228360",
"Ship Via": "FedEx Ground",
"ItemName": "WP36-HON-3P",
"Quantity": "1",
"Document Number": "IF120193",
"SellBrite Order Number": "13999",
"Carrier": "Standard"
}
]
]
}


And this template:

{
"shipment":
{{#each data}}
{{#if @index}} , {{/if}}
{
"sb_order_seq": {{this.0.[SellBriteOrderNumber]}},
"tracking_number": "{{this.0.TrackingNumber}}",
"carrier_name": "FedEx",
"tracking_url": "",
"warehouse_uuid": "b02fa8bc-9b01-43de-b9e7-f69ead1b9a98",
"shipping_method": "FedEx Ground",
"items": [
{{#each this}}
{{#if @index}} , {{/if}}
{
"sku": "{{ItemName}}",
"quantity": {{Quantity}}
}
{{/each}}
]
}
{{/each}}
}

I would like to output just one...
{
"sku": "{{ItemName}}",
"quantity": {{Quantity}}
}
 
...block but sum the {{Quantity}} for all iterations in the loop.
 
The host we are sending this to can't accept multpile tracking numbers for the same SKU so I want to add all quantities shipped to the first tracking number ({{this.0.TrackingNumber}})
 
Any thoughts?
0

Comments

2 comments
Date Votes
  • Official comment

    I don't see how this can be done with handlebars because you basically need to regroup your data. It looks like this data is coming from NetSuite, could you group the data on the NetSuite saved search side? If you can get one tracking number tied to the item fulfillment on the result, then you could group by item and sum quantity. If that's not possible, I made a pre save page script that you could utilize.

    function preSavePage (options) {
      
      let newData = [];
      for (let d of options.data) {
        
        //get a list of unique skus to later sum to
        let uniqueSKUs = [];
        for (let r of d) {
          if (uniqueSKUs.indexOf(r.ItemName) === -1) {
            uniqueSKUs.push(r.ItemName);
          }
        }
        
        let newRec = [];
        for (let s of uniqueSKUs) {
          let o = {};
          let c = 0;
          for (let r of d) {
            if (s == r.ItemName && c === 0) {
              o = JSON.parse(JSON.stringify(r));
              o.TrackingNumbers = [];
              o.TrackingNumbers.push(r.TrackingNumber);
              c++;
            } else if (s == r.ItemName && c > 0) {
              o.Quantity = (Number(o.Quantity)+Number(r.Quantity)).toFixed(0);
              if (o.TrackingNumbers.indexOf(r.TrackingNumber) === -1) {
                o.TrackingNumbers.push(r.TrackingNumber);
              }
            }
          }
          o.TrackingNumbers = o.TrackingNumbers.join(',');
          newRec.push(o);
        }
        
        newData.push(newRec);
      }
      
      return {
        data: newData,
        errors: options.errors,
        abort: false,
        newErrorsAndRetryData: []
      }
    }
  • Thanks Tyler I will give that a shot. I was having some issues getting the Netsuite saved search to report out the data in a way I could use it. The package contents are a custom record type under the fulfillment record and getting all the data the way I wanted proved difficult. I will give the preSavePage a shot, thanks!

    0

Please sign in to leave a comment.

 

Didn't find what you were looking for?

New post