Hi Scott,
Thanks for the direction, I have thrown it into ChatGPT and got exactly what I needed for the flow, after a few tweaks to the prompts.
For anyone who comes across this, and would like to know the solution I got to in the end.
Using Javascript transform…
function transform(options) {
const formatDateString = (dateStr) => {
const [day, month, year] = dateStr.split("/");
return `${year}-${month}-${day}`;
};
const data = options.record; // Input JSON data
const transformed = {
ShipmentID: data[0].ShipmentID,
Carrier: data[0].Carrier,
Reference: data[0].Reference,
ShipmentDate: formatDateString(data[0].ShipmentDate),
DeliveryDate: formatDateString(data[0].DeliveryDate),
DeliveryTime: data[0].DeliveryTime,
OrderNumber: data[0].OrderNumber,
CustomerPO: data[0].CustomerPO,
OrderMemo: data[0].OrderMemo,
LineDetails: ,
OrderTotals: {
TotalUnits: 0,
TotalBoxes: 0,
TotalPallets: 0,
TotalProducts: 0
},
UniqueSKUs: new Set() // Temporary set to track unique SKUs
};
// Process each line
data.forEach(item => {
const {
Pallet,
SKU,
Batch,
Expiry,
UnitsinBox,
TotalUnits,
BoxesperPallet,
“Pallet Weight”: PalletWeight,
Dimensions
} = item;
// Add LineDetails
transformed.LineDetails.push({
Pallet,
SKU,
Batch,
Expiry: formatDateString(Expiry),
UnitsinBox,
TotalUnits: parseInt(TotalUnits, 10),
BoxesperPallet: parseInt(BoxesperPallet, 10),
PalletWeight,
Dimensions
});
// Update OrderTotals
transformed.OrderTotals.TotalUnits += parseInt(TotalUnits, 10);
transformed.OrderTotals.TotalBoxes += parseInt(BoxesperPallet, 10);
transformed.OrderTotals.TotalPallets += 1;
// Track unique SKUs
transformed.UniqueSKUs.add(SKU);
});
// Finalize TotalProducts by counting unique SKUs
transformed.OrderTotals.TotalProducts = transformed.UniqueSKUs.size;
delete transformed.UniqueSKUs; // Remove temporary set
// Assign the transformed data to options.record
options.record = [transformed]; // Single record in an array
return options;
}