I have a script that validates if inventory detail...
# suitescript
m
I have a script that validates if inventory details on a SO has the expiration date. However, when an item does have the inventory details but is not set, the script falls over. Or at least the screen shows the error: "You cannot submit this form due to an unexpected error." I have tried skipping this scenario but I am struggling to get past this error. This is my client script (saveRecord) function. Appreciate any ideas.
Copy code
function saveRecord(context) {
        const currentRec = currentRecord.get();
        log.debug({title: "Current Record", details: JSON.stringify(currentRec)});

        const lineCount = currentRec.getLineCount({sublistId: 'item'});
        log.debug({title: "Line Count", details: lineCount});

        if (lineCount === 0) {
            log.debug({title: "No lines in the item sublist to process.", details: lineCount});
            return true; // Skipping further processing if no lines
        }

        for (let i = 0; i < lineCount; i++) {
            log.debug("Inside loop for line " + i);

            const itemType = currentRec.getSublistValue({
                sublistId: 'item',
                fieldId: 'itemtype',
                line: i
            });

            log.debug({title: "Item Type for line " + i, details: itemType});

            if (isInventoryItem(itemType)) {
                log.debug("Processing inventory item for line " + i);

                currentRec.selectLine({
                    sublistId: 'item',
                    line: i
                });

                let inventoryDetailSubrecord = null;
                try {
                    inventoryDetailSubrecord = currentRec.getCurrentSublistSubrecord({
                        sublistId: 'item',
                        fieldId: 'inventorydetail',
                        line: i
                    });

                    // If no inventory detail subrecord, skip and continue
                    if (!inventoryDetailSubrecord) {
                        log.debug({
                            title: "No inventory detail for line " + i,
                            details: "Skipping line due to missing inventory detail."
                        });
                        continue; // Skip this line if no inventory detail exists
                    }

                    log.debug({
                        title: "Inventory Detail Subrecord for line " + i,
                        details: JSON.stringify(inventoryDetailSubrecord)
                    });
                } catch (error) {
                    log.error({
                        title: "Error retrieving inventory detail for line " + i,
                        details: error.message
                    });
                    continue; // Skip this line if error occurs in fetching subrecord
                }

                let inventoryAssignmentLineCount = 0;
                try {
                    inventoryAssignmentLineCount = inventoryDetailSubrecord.getLineCount({
                        sublistId: 'inventoryassignment'
                    });

                    // If no inventory assignment lines, skip this line
                    if (inventoryAssignmentLineCount === 0) {
                        log.debug({
                            title: "No inventory assignments for line " + i,
                            details: "Skipping line due to zero inventory assignments."
                        });
                        continue;
                    }

                    log.debug({
                        title: "Inventory Assignment Line Count for line " + i,
                        details: inventoryAssignmentLineCount
                    });
                } catch (error) {
                    log.error({
                        title: "Error retrieving line count for inventory assignment sublist for line " + i,
                        details: error.message
                    });
                    continue; // Skip this line if error occurs in line count retrieval
                }

                for (let j = 0; j < inventoryAssignmentLineCount; j++) {
                    let serialNumber = null;
                    let expirationDate = null;

                    try {
                        serialNumber = inventoryDetailSubrecord.getSublistValue({
                            sublistId: 'inventoryassignment',
                            fieldId: 'issueinventorynumber',
                            line: j
                        });

                        expirationDate = inventoryDetailSubrecord.getSublistValue({
                            sublistId: 'inventoryassignment',
                            fieldId: 'expirationdate',
                            line: j
                        });

                        log.debug({
                            title: "Serial Number and Expiration Date for line " + i + " | Assignment line " + j,
                            details: "Serial Number: " + serialNumber + " | Expiration Date: " + expirationDate
                        });
                    } catch (error) {
                        log.error({
                            title: "Error retrieving inventory assignment details for line " + i + " | Assignment line " + j,
                            details: error.message
                        });
                        continue; // Skip this inventory assignment line if any error occurs
                    }

                    if (!expirationDate) {
                        log.debug({
                            title: "Missing expiration date for inventory assignment line " + j,
                            details: "Line " + i + " | Assignment line " + j
                        });
                        dialog.alert({
                            title: missingExpirationDateTitle,
                            message: missingExpirationDateMessage
                        });
                        return false; // Prevent save
                    }
                }
            } else {
                log.debug("Skipping non-inventory item for line " + i);
            }
        }

        return true;
    }