You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NetSuite脚本修改:添加判断忽略Backorder项避免报错

解决NetSuite Invoice中Backorder项导致的批次号脚本错误

问题根源

你的UserEvent脚本在处理Invoice的Backorder行时,尝试访问不存在的inventorydetail(库存明细)记录,NetSuite对无效的库存明细ID会抛出"Invalid number (must be positive)"错误——因为Backorder项没有实际出库,自然没有对应的库存明细数据。

修改方案

在脚本遍历Item子列表时,先判断当前行是否为Backorder项,直接跳过这类行的库存明细/批次号处理逻辑即可。

修改后的代码示例

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/record', 'N/log'], function(record, log) {
    function beforeSubmit(context) {
        var currentRec = context.newRecord;
        var recType = currentRec.type;

        // 仅处理Item Fulfilment和Invoice
        if (recType !== record.Type.ITEM_FULFILMENT && recType !== record.Type.INVOICE) return;

        var lineCount = currentRec.getLineCount({sublistId: 'item'});

        for (var i = 0; i < lineCount; i++) {
            // 检查当前行是否为Backorder项(仅Invoice有此字段)
            var isBackorder = currentRec.getSublistValue({
                sublistId: 'item',
                fieldId: 'isbackorder',
                line: i
            });

            // 跳过Backorder行的处理
            if (isBackorder) {
                log.debug({title: 'Skipped Backorder Line', details: `Line ${i} is a backorder item, no inventory detail available`});
                continue;
            }

            // 原有批次号处理逻辑(仅针对非Backorder行)
            var invDetailId = currentRec.getSublistValue({
                sublistId: 'item',
                fieldId: 'inventorydetail',
                line: i
            });

            if (invDetailId) {
                var invDetailRec = record.load({type: record.Type.INVENTORY_DETAIL, id: invDetailId});
                // 获取批次号(根据你的实际库存分配逻辑调整)
                var lotNum = invDetailRec.getSublistValue({
                    sublistId: 'inventoryassignment',
                    fieldId: 'issueinventorynumber',
                    line: 0
                });

                if (lotNum) {
                    currentRec.setSublistValue({
                        sublistId: 'item',
                        fieldId: 'custcol_your_lot_number_field', // 替换为你的自定义批次号字段ID
                        line: i,
                        value: lotNum
                    });
                }
            }
        }
    }

    return {
        beforeSubmit: beforeSubmit
    };
});

关键说明

  1. Backorder判断逻辑:NetSuite的Item子列表中,isbackorder字段是布尔类型,专门标记Invoice中的Backorder项,直接用这个字段判断最准确。
  2. 权限检查:确保脚本部署的权限集包含isbackorder字段的访问权限,否则会导致字段读取失败。
  3. 兼容Item Fulfilment:Item Fulfilment不会有Backorder行,原有逻辑不受影响,仍能正常处理已出库的批次号。
  4. 字段替换:将代码中的custcol_your_lot_number_field替换为你实际使用的自定义批次号列ID。

可选优化

如果需要更严谨的判断,可以结合quantityfulfilled字段,过滤掉所有未出库的项:

var qtyFulfilled = currentRec.getSublistValue({sublistId: 'item', fieldId: 'quantityfulfilled', line: i});
if (isBackorder || qtyFulfilled <= 0) {
    continue;
}

内容的提问来源于stack exchange,提问作者Trav OZ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 22:50:37