Function to Search and Find the Available Items and Available Item Quantity in a Bin

 /**
         * Function defined to fetch all the items available in the bin in the corresponding location
         * @param {Number} bin 
         * @param {Number} location 
         * @returns {Object}
         */
        function findAvailableItems(bin, location) {
            try {
                let binItemCombQtyObj = {}
                let itemSearchObj = search.create({
                    type: "item",
                    filters:
                        [
                            ["inventorydetail.binnumber", "anyof", bin],
                            "AND",
                            ["inventorydetail.location", "anyof", location],
                            "AND",
                            ["sum(inventorydetail.itemcount)", "greaterthan", "0"]
                        ],
                    columns:
                        [
                            search.createColumn({
                                name: "binnumber",
                                join: "inventoryDetail",
                                summary: "GROUP",
                                sort: search.Sort.ASC,
                                label: "Bin Number"
                            }),
                            search.createColumn({
                                name: "item",
                                join: "inventoryDetail",
                                summary: "GROUP",
                                label: "Item"
                            }),
                            search.createColumn({
                                name: "itemcount",
                                join: "inventoryDetail",
                                summary: "SUM",
                                label: "Item Count"
                            }),
                            search.createColumn({
                                name: "location",
                                join: "inventoryDetail",
                                summary: "GROUP",
                                label: "Location"
                            }),
                            search.createColumn({
                                name: "quantity",
                                join: "inventoryDetail",
                                summary: "SUM",
                                label: "Quantity"
                            })
                        ]
                });
                itemSearchObj.run().each(function (result) {
                    // .run().each has a limit of 4,000 results
                    let bin = result.getValue({
                        name: "binnumber",
                        join: "inventoryDetail",
                        summary: "GROUP",
                        sort: search.Sort.ASC,
                        label: "Bin Number"
                    });
                    let item = result.getValue({
                        name: "item",
                        join: "inventoryDetail",
                        summary: "GROUP",
                        label: "Item"
                    });

                    binItemCombQtyObj[`${bin}-${item}`] = result.getValue({
                        name: "itemcount",
                        join: "inventoryDetail",
                        summary: "SUM",
                        label: "Item Count"
                    });
                    return true;
                });

                return binItemCombQtyObj;
            } catch (e) {
                log.error('error@findAvailableItems', e);
                return {};
            }
        }

Leave a comment

Your email address will not be published. Required fields are marked *