How to check if order contains items fulfilled from more than 1 location

Hello,

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.

Any help would be gratefully received.

Many thanks,

Darren.

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)

Count (order/fulfillment orders)

Condition: If count >= 2

True: Add order tag

Hi Paul,

Thank you for your reply and I had already come to a similar conclusion. I can create all of the above except the run code to do the counting part.

Have you achieved this?

Many thanks,

Darren.

I lightly tested this, but something like…

Query:

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!]!
}