Edgar Valdes
11/03/2023, 4:36 PMgetInputData
I got a search result with several columns:
CustomerID, ItemID, Quantity, Price, etc.
Customer 1, Item 1, Quantity 3, Price 9.99
Customer 1, Item 2, Quantity 2, Price 10.99
Customer 2, Item 1, Quantity 3, Price 5.99
In the Map
stage I need to group by CustomerID, in order to create the SO in the Reduce
stage.
How do I pass the grouped values from Map
to Reduce
?Nathan L
11/03/2023, 4:43 PMmapContext.write({key: customerId, value: itemDataObj})
That will give you a reduce context that looks like this
reduceContext = {values: customerId, values: [{itemData1}, {itemData2}]}
So your key in reduceContext will be the customerId, and values will be an array of the item objects you wrote into the value of mapContextNathan L
11/03/2023, 4:49 PMgetInputDate(inputContext) {
return search.load({id: searchId})
}
map(mapContext) {
const key = mapContext.key;
const value = JSON.parse(mapContext.value);
const customerId = //do whatever you need to get the customer id
const itemData = {} // populate this object with your item data
// utilizes the native map/reduce functionality to group by customer id
mapContext.write({key: customerId, value: itemData});
}
reduce(reduceContext) {
const customerId = reduceContext.key;
const arrayOfItemDataObjects = reduceContext.values.map(JSON.parse);
// ... do whatever processing on the data you need
}
erictgrubaugh
11/03/2023, 4:50 PMreduceContext.values
is an Array, not a JSON string, so
const arrayOfItemDataObjects = reduceContext.values.map(JSON.parse);
erictgrubaugh
11/03/2023, 4:50 PMNathan L
11/03/2023, 4:50 PMerictgrubaugh
11/03/2023, 4:50 PMEdgar Valdes
11/03/2023, 6:57 PM