An unexpected error occurred while saving the Sales Order, specifically during the update of inventory details and line-level location.

 First, the line-level location is updated and the order is saved. Then, the order is reloaded, and the inventory details are saved. This solution has resolved the issue.

   /**
 * Updates the sales order with the specified lines.
 * @param {string} itemReceiptId - The ID of the item receipt.
 * @param {string} salesOrderId - The ID of the sales order to update.
 * @param {Array} linesToUpdate - Array of line items to update in the sales order.
 */
    const updateSalesOrder = (itemReceiptId, salesOrderId, linesToUpdate) => {
        try {
            // Load the sales order once before processing
            let salesOrder = record.load({
                type: record.Type.SALES_ORDER,
                id: salesOrderId,
                isDynamic: true
            });
    
            // Loop to update location for the lines
            for (let lineData of linesToUpdate) {
                let lineIndex = salesOrder.findSublistLineWithValue({
                    sublistId: 'item',
                    fieldId: 'custcol_jj_dt_order_line',
                    value: lineData.soLineNumber
                });
    
                if (lineIndex !== -1) {
                    salesOrder.selectLine({ sublistId: 'item', line: lineIndex });
                    salesOrder.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'location',
                        value: lineData.destinationLocation,
                        ignoreFieldChange: false,
                        forceSyncSourcing: true
                    });
                    salesOrder.commitLine({ sublistId: 'item' });
                } else {
                    // Log error if the line is not found
                    errorLog.push({
                        itemReceiptId: itemReceiptId || "",
                        salesorder: salesOrderId || "",
                        itemName: lineData.itemName || "",
                        error: "The SO Line # is missing or not found in Sales order"
                    });
                }
            }
    
            // Save the sales order after updating locations
            salesOrder.save({ enableSourcing: true, ignoreMandatoryFields: true });
    
            // Reload the sales order to update inventory details
            salesOrder = record.load({
                type: record.Type.SALES_ORDER,
                id: salesOrderId,
                isDynamic: true
            });
    
            // Loop to update inventory details
            for (let lineData of linesToUpdate) {
                log.debug("lineData",lineData)
                let lineIndex = salesOrder.findSublistLineWithValue({
                    sublistId: 'item',
                    fieldId: 'custcol_jj_dt_order_line',
                    value: lineData.soLineNumber
                });
    
                if (lineIndex !== -1) {
                    salesOrder.selectLine({ sublistId: 'item', line: lineIndex });
                    let inventoryDetail = lineData.inventoryDetail
                    let inventoryAssignmentCount = inventoryDetail.getLineCount({ sublistId: 'inventoryassignment' });
                   
                    if(inventoryAssignmentCount>0)
                    {
                    let soInventoryDetail = salesOrder.getCurrentSublistSubrecord({
                        sublistId: 'item',
                        fieldId: 'inventorydetail'
                    }); 
                    updateSalesOrderLine(itemReceiptId, salesOrder, soInventoryDetail, lineData.inventoryDetail);
                    }
                } 
                else {
                    log.error("error")
                    // Log error if the line is not found
                    errorLog.push({
                        itemReceiptId: itemReceiptId || "",
                        salesorder: salesOrderId || "",
                        itemName: lineData.itemName || "",
                        error: "The SO Line # is missing or not found in Sales order during inventory update"
                    });
                }
            }
    
            // Save the sales order after updating inventory details
            salesOrder.save({ enableSourcing: true, ignoreMandatoryFields: true });
    
        } catch (e) {
            log.error("error3333",e)
            let itemNames = linesToUpdate.map(item => item.itemName).join('*');
            errorLog.push({
                itemReceiptId: itemReceiptId || "",
                salesorder: salesOrderId || "",
                itemName: itemNames || "",
                error: e.message || ""
            });
        }
    };
    /**
  * Updates a specific line in the sales order.
  * @param {string} itemReceiptId - The ID of the item receipt.
  * @param {Record} salesOrder - The sales order record to update.
  * @param {string} soDtLine - The line number of the sales order to update.
  * @param {string} destinationLocation - The new location for the line item.
  * @param {Record} inventoryDetail - The inventory detail record for the line item.
  */
    const updateSalesOrderLine = (itemReceiptId, salesOrder, soInventoryDetail, inventoryDetail) => {
        let itemName;
        try {
            let inventoryAssignmentCount = inventoryDetail.getLineCount({ sublistId: 'inventoryassignment' });
            log.debug("inside updateLine")
            for (let i = 0; i < inventoryAssignmentCount; i++) {
                let quantity = inventoryDetail.getSublistValue({
                    sublistId: 'inventoryassignment',
                    fieldId: 'quantity',
                    line: i
                });
                let serialOrLotNumber = inventoryDetail.getSublistValue({
                    sublistId: 'inventoryassignment',
                    fieldId: 'receiptinventorynumber',
                    line: i
                });
    log.debug("ir quantity",quantity)
    log.debug("ir serialOrLotNumber",serialOrLotNumber)
                if (quantity && serialOrLotNumber) {
                    log.debug("inside qty")
                    soInventoryDetail.selectNewLine({ sublistId: 'inventoryassignment' });
                    soInventoryDetail.setCurrentSublistValue({
                        sublistId: 'inventoryassignment',
                        fieldId: 'quantity',
                        value: quantity
                    });
                    soInventoryDetail.setCurrentSublistValue({
                        sublistId: 'inventoryassignment',
                        fieldId: 'receiptinventorynumber',
                        value: serialOrLotNumber
                    });
                    soInventoryDetail.commitLine({ sublistId: 'inventoryassignment' });
                }
            }
    
            // Commit the inventory detail update
            salesOrder.commitLine({ sublistId: 'item' });
    
        } catch (e) {
            log.error("error@@@@@",e)
            itemName = salesOrder.getCurrentSublistText({
                sublistId: 'item',
                fieldId: 'item'
            });
            errorLog.push({
                itemReceiptId: itemReceiptId,
                salesorder: salesOrder.id,
                itemName: itemName,
                error: e.message
            });
        }
    };

Leave a comment

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