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

SuiteScript 2.x参数传递排查:多搜索取值更新记录

代码问题排查与修正

核心问题总结

  • 参数传递错误:函数调用未传递必要参数,导致变量未定义
  • 变量作用域问题:局部变量未声明、未初始化就使用
  • 语法错误:forEach循环闭合符号错误、函数参数定义与调用不匹配
  • 逻辑错误:搜索条件未关联目标数据、更新操作使用错误值

修正后的完整代码

/**
 * 
 * @NApiVersion 2.x
 * @NScriptType ScheduledScript
 * @NModuleScope SameAccount
 */
define(['N/record', 'N/search', 'N/log'],

function(record, search, log) {

    /**
     * 获取符合条件的Item Fulfillment记录
     * @returns {Array} 包含Fulfillment ID、行号、匹配名称的对象数组
     */
    function getfulfillinfo(){
        var itemfulfillmentSearchObj = search.create({
            type: "itemfulfillment",
            filters: [
                ["type","anyof","ItemShip"], 
                "AND", 
                ["mainline","is","F"], 
                "AND", 
                ["item.type","anyof","InvtPart"], 
                "AND", 
                ["item.custitemmedium","noneof","94","95"], 
                "AND", 
                ["account","anyof","236","239"], 
                "AND", 
                ["createdfrom.type","anyof","SalesOrd"], 
                "AND", 
                ["itemnumber.custitemnumbereditionconsigned","is","T"], 
                "AND", 
                ["trandate","within","03/13/2023"]
            ],
            columns: [
                search.createColumn({name: "line", label: "Line"}),
                search.createColumn({name: "item", label: "Item"}),
                search.createColumn({name: "serialnumbers", label: "Serial Numbers"}),
                search.createColumn({
                    name: "formulatext",
                    formula: "CONCAT(CONCAT(SUBSTR({item.name},INSTR({item.name},':')+2),'-'),{serialnumbers})",
                    label: "Matching Name"
                }),
                search.createColumn({
                    name: "custitemnumbereditionconsigned",
                    join: "itemNumber",
                    label: "Consigned Edition"
                })
            ]
        });

        var fulfillinfo = [];
        var searchResult = itemfulfillmentSearchObj.run().getRange({start: 0, end: 10});

        searchResult.forEach(function(result) {
            let obj = {};
            obj.Name = result.getValue({name: "formulatext"});
            obj.FulfillId = result.id;
            obj.LineNum = parseInt(result.getValue({name: "line"}));
            fulfillinfo.push(obj);
        });

        log.debug({title: "Fulfillment Results", details: fulfillinfo});
        return fulfillinfo;
    }

    /**
     * 根据匹配名称查找Consignment Tracker记录ID
     * @param {string} matchName - 商品名+序列号的组合名称
     * @returns {string|null} Consignment Tracker的内部ID,未找到则返回null
     */
    function getCTid(matchName){
        if(!matchName){
            log.error({title: "Missing Parameter", details: "matchName is required"});
            return null;
        }

        var customrecordconsignmenttrackerSearchObj = search.create({
            type: "customrecordconsignmenttracker",
            filters: [
                ["isinactive","is","F"],
                "AND",
                ["name","is", matchName]
            ],
            columns: ["internalid"]
        });

        var CTResult = customrecordconsignmenttrackerSearchObj.run().getRange(0,1);
        
        log.debug({title: "CT Search Result", details: CTResult});
        
        let CTid = null;
        if(CTResult.length > 0){
            CTid = CTResult[0].getValue({name: "internalid"});
        }
        return CTid;
    }

    /**
     * 更新Item Fulfillment记录,关联Consignment Tracker
     * @param {string} fulfillId - Item Fulfillment的内部ID
     * @param {number} lineNum - 要更新的子列表行号
     * @param {string} ctId - Consignment Tracker的内部ID
     * @returns {string|null} 更新后的记录ID,失败则返回null
     */
    function updateFulfill(fulfillId, lineNum, ctId) {
        if(!fulfillId || !lineNum || !ctId){
            log.error({title: "Missing Parameters", details: "fulfillId, lineNum, ctId are required"});
            return null;
        }

        try {
            var CTFulfill = record.load({
                type: record.Type.ITEM_FULFILLMENT, 
                id: fulfillId,
                isDynamic: true
            });

            CTFulfill.selectLine({
                sublistId: 'item',
                line: lineNum
            });
            
            log.debug({title: "Selected Line", details: lineNum});
            
            CTFulfill.setCurrentSublistValue({
                sublistId: 'item',
                fieldId: 'custcoltranconsignmenttracker',
                value: ctId,
                ignoreFieldChange: true,
                forceSyncSourcing: true
            });

            CTFulfill.commitLine({sublistId: 'item'});

            var savedId = CTFulfill.save({enableSourcing: true});
            log.debug({title: "Fulfillment Updated", details: savedId});
            return savedId;
        } catch (e) {
            log.error({title: "Update Failed", details: e.message});
            return null;
        }
    }

    /**
     * 调度脚本入口函数
     * @param {Object} scriptContext
     */
    function execute(scriptContext) {
        var fulfillList = getfulfillinfo();
        
        if(fulfillList.length === 0){
            log.debug({title: "No Records", details: "No eligible Item Fulfillment records found"});
            return;
        }

        for (var i = 0; i < fulfillList.length; i++) {
            let currentItem = fulfillList[i];
            log.debug({title: "Processing Item", details: currentItem});
            
            let ctId = getCTid(currentItem.Name);
            
            if(ctId){
                updateFulfill(currentItem.FulfillId, currentItem.LineNum, ctId);
            } else {
                log.warning({title: "CT Not Found", details: `No Consignment Tracker found for name: ${currentItem.Name}`});
            }
        }
    }

    return {execute: execute};
});

关键修正点说明

  1. 参数传递优化:

    • getCTid改为接收单个匹配名称参数,逻辑更聚焦
    • updateFulfill明确接收Fulfillment ID、行号、CT ID三个必要参数
    • 入口函数遍历搜索结果数组,逐个传递参数执行关联操作
  2. 语法与作用域修复:

    • 修复forEach循环的闭合符号错误
    • 使用let声明局部变量,避免全局作用域污染
    • 确保变量初始化后再使用,消除未定义错误
  3. 逻辑准确性提升:

    • Consignment Tracker搜索使用精确匹配(is)替代模糊匹配,避免关联错误记录
    • 动态模式下更新子列表时,无需重复指定行号,已通过selectLine选中目标行
    • 添加错误捕获与日志,便于定位问题
  4. 代码可读性优化:

    • 修正函数注释,准确描述参数与返回值
    • 重命名变量提升语义清晰度
    • 添加关键步骤日志,方便追踪执行流程

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 20:17:11