NettleRick
NettleRick

Reputation: 1

How to perform record.transform from work order into assembly build when there is multiple lot numbered items

I am working with SuiteScript to transform work orders into assembly builds, and I encountered challenges due to the multiple lot numbers associated with lot- items. My logic needs to ensure that all lot numbers are fully used up in a specific lot items.

For example, if the “Chicken Rice” (a lot-item) requires 100 quantity for assembly, and there are two lot numbers (A001 and A002), where lot A001 has 60 quantity and lot A002 has 150 quantity, the process should first consume the 60 quantity from lot A001, and then use the remaining 40 quantity from lot A002 to fulfill the required 100 quantity for the assembly build.

How can I implement this logic in my script?

`// Get available Lot Number, could be multiple const lotNumbers = _util.getAvailableLotNumbers(itemId, qtyToBuild, location,units);

                    if (lotNumbers.length > 0) {
                        log.debug(‘Found Lot Numbers’, lotNumbers);
                        let remainingQtyToBuild = qtyToBuild;


                        try {
                            let inventoryDetail = assemblyBuild.getCurrentSublistSubrecord({
                                sublistId: ‘component’,
                                fieldId: ‘componentinventorydetail’,
                            });
                            log.debug(‘Inventory Detail sublist fields’, inventoryDetail);
                            // Loop through the lot numbers and assign quantities until qtyToBuild is met
                            for (let i = 0; i < lotNumbers.length && remainingQtyToBuild > 0; i++) {
                                let currentLot = lotNumbers[i];
                                let lotQtyAvailable = currentLot.quantityUsed;


                                // Calculate how much quantity to assign from this lot
                                let qtyToAssign = Math.min(remainingQtyToBuild, lotQtyAvailable);


                                // Select a new line in the inventory assignment sublist
                                inventoryDetail.selectNewLine({
                                    sublistId: ‘inventoryassignment’
                                });


                                // Set the lot number (issueinventorynumber)
                                log.debug(‘Current lot number id’, currentLot.lotNumberId);
                                inventoryDetail.setCurrentSublistValue({
                                    sublistId: ‘inventoryassignment’,
                                    fieldId: ‘issueinventorynumber’,
                                    value: currentLot.lotNumberId
                                });


                                // Set the quantity for this lot
                                inventoryDetail.setCurrentSublistValue({
                                    sublistId: ‘inventoryassignment’,
                                    fieldId: ‘quantity’,
                                    value: qtyToAssign
                                });


                                // Commit the current line in the sublist
                                inventoryDetail.commitLine({
                                    sublistId: ‘inventoryassignment’
                                });


                                // Subtract the assigned quantity from the remaining required quantity
                                remainingQtyToBuild -= qtyToAssign;


                                log.debug(`Assigned ${qtyToAssign} from Lot ${currentLot.lotNumberId}`, `Remaining qty to build: ${remainingQtyToBuild}`);
                            }


                            // If there’s any remaining quantity that couldn’t be fulfilled, throw an error
                            if (remainingQtyToBuild > 0) {
                                throw new Error(`Insufficient quantity across all lots for Item ID: ${itemId}. Unable to fulfill the required quantity of ${qtyToBuild}.`);
                            }`

This is my current logic in SuiteScript to ensure that all lot numbers are fully used. However, I’m still encountering an error because the system use only the first lot number and doesn’t move on to the second lot number when the lot item quantity is not fully fulfilled yet.

If anyone has experience with this, I would really appreciate your help!

Upvotes: 0

Views: 23

Answers (0)

Related Questions