Skip to content

Instantly share code, notes, and snippets.

@mikeacjones
Last active January 6, 2021 16:39
Show Gist options
  • Save mikeacjones/894570c6c6f856b8f6d2c9145a43de9f to your computer and use it in GitHub Desktop.
Save mikeacjones/894570c6c6f856b8f6d2c9145a43de9f to your computer and use it in GitHub Desktop.
Example hlLoop for EDI - elements use indexing and back-reference relative indexes when building structure. See hlLoop_usage.dwl for example
%dw 2.0
fun hlLoop(payload: Object) =
((payload.DeliveryItems distinctBy $.PurchaseOrder).PurchaseOrder reduce ((order, accum=hlLoopRoot(payload)) ->
payload.DeliveryItems distinctBy $.ProductId reduce ((item, accum=hlLoopPurchaseOrder(order, accum)) ->
(payload.DeliveryItems filter ($.ProductId == item.ProductId and $.ProductBatchNumber != null)) reduce ((package, accum=hlLoopItem(item, accum)) ->
hlLoopPackage(package, accum)
)
)
)).items
fun hlLoopRoot(payload: Object) =
{
index: 1,
poIndex: 1,
items: [{
"010_HL": {
HL01: "1",
HL03: "S",
},
"120_TD5": [{
TD501: "O",
TD502: "2",
TD503: "HMES",
TD504: "M"
}],
"130_TD3": [{
TD301: "TF",
TD303: "999999"
}],
"150_REF": [{
REF01: "BM",
REF02: payload.salesOrder
}],
"220_N1_Loop": [{
"220_N1": {
N101: "ST",
N102: payload.Destination.DestinaionName
},
"240_N3": [{
N301: payload.Destination.DestinationAddressLine1
}],
"250_N4": {
N401: payload.Destination.DestinationCity,
N402: payload.Destination.DestinationState,
N403: payload.Destination.DestinationZipCode
}
},
{
"220_N1": {
N101: "SO",
N102: payload.Customer.CustomerName
},
"240_N3": [{
N301: payload.Customer.CustomerAddressLine1
}],
"250_N4": {
N401: payload.Customer.CustomerCity,
N402: payload.Customer.CustomerState,
N403: payload.Customer.CustomerZipCode
}
}]
}]
}
fun hlLoopPurchaseOrder(order, accum: Object) =
{
items: accum.items ++
[{
"010_HL": {
HL01: accum.index + 1,
HL02: accum.poIndex,
HL03: 0
},
"050_PRF": {
"PRF01": order
}
}],
index: accum.index + 1,
poIndex: accum.poIndex,
oIndex: accum.index + 1
}
fun hlLoopItem(item, accum: Object) =
{
index: accum.index + 1,
poIndex: accum.poIndex,
oIndex: accum.oIndex,
iIndex: accum.index + 1,
items: accum.items ++ [{
"010_HL": {
HL01: accum.index + 1,
HL02: accum.oIndex,
HL03: "I"
},
"020_LIN": {
LIN01: "001",
LIN02: "VP",
LIN03: item.ProductId,
LIN04: "BP",
LIN05: item.ProductQualifier
},
"030_SN1": {
SN101: "001",
SN102: item.ProductQuantity,
SN103: "PA"
},
"070_PID": [{
PID01: "F",
PID02: "08",
PID03: "PA",
PID05: item.ProductDescription
}]
}]
}
fun hlLoopPackage(package, accum: Object) =
{
index: accum.index + 1,
poIndex: accum.poIndex,
oIndex: accum.oIndex,
iIndex: accum.iIndex,
items: accum.items ++ [{
"010_HL": {
HL01: accum.index + 1,
HL02: accum.iIndex,
HL03: "P"
},
"020_LIN": {
LIN02: "LT",
LIN03: package.ProductBatchNumber default ""
},
"030_SN1": {
SN102: package.ProductDeliveredQuantitySU as Number,
SN103: "PA"
},
"080_MEA": [{
MEA01: "WT",
MEA02: "G",
MEA03: package.ProductGrossWeight as Number {format: "#.###"},
MEA0401: "KG"
},
{
MEA01: "WT",
MEA02: "N",
MEA03: package.ProductDeliveredQuantitySKU as Number {format: "#.###"},
MEA0401: "KG"
}]
}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment