Can anyone help me with some thoughts on how to create a flow that is triggered when an order is created and that adds a tag to the order if the order contains line items that are fulfilled from more than 1 location. For example, the order contains a pencil and a pen, and the pencil is fulfilled from warehouse location 1 while the pen is fulfilled from warehouse location 2.
Yes it’s possible. If you ever split fulfillment for a single location you might need to use run code to do that counting part, because you cannot rely on counting order / fulfillmentOrders. If you don’t split fulfillment that way:
Order created
Wait 5 min (for fulfilllment orders to be assigned)
query {
order {
id
name
fulfillmentOrders {
id
assignedLocation {
location {
id
name
}
}
}
}
}
Code:
export default function main(input) {
// Get the order data from the input
const order = input.order;
// Get the fulfillment orders from the order
const fulfillmentOrders = order.fulfillmentOrders;
// Create a set to store unique location IDs
const uniqueLocationIds = new Set();
// Iterate over each fulfillment order and add its location ID to the set
fulfillmentOrders.forEach(fulfillmentOrder => {
if (fulfillmentOrder.assignedLocation && fulfillmentOrder.assignedLocation.location) {
uniqueLocationIds.add(fulfillmentOrder.assignedLocation.location.id);
}
});
// Count the number of unique locations
const locationCount = uniqueLocationIds.size;
// Create an array of location names for display purposes
const locationNames = [];
fulfillmentOrders.forEach(fulfillmentOrder => {
if (fulfillmentOrder.assignedLocation &&
fulfillmentOrder.assignedLocation.location &&
!locationNames.includes(fulfillmentOrder.assignedLocation.location.name)) {
locationNames.push(fulfillmentOrder.assignedLocation.location.name);
}
});
// Return the result with the location count and details
return {
locationCount: locationCount,
orderName: order.name,
locations: locationNames
};
}
Output:
"Represents the result of counting fulfillment order locations"
type Output {
"The number of unique fulfillment locations for the order"
locationCount: Int!
"The name/number of the order"
orderName: String!
"List of unique location names for the order"
locations: [String!]!
}