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

如何在NetSuite Map Reduce脚本中取消多选字段的指定客户值

问题:NetSuite Map Reduce脚本无法取消多选字段中的指定客户

我正尝试在NetSuite Map Reduce脚本中,取消特定项目的多选字段(custitemcustomer)里的指定客户。使用setValue方法设置新的客户列表,但该方法无法取消选中目标客户。以下是相关代码,恳请提供建议。

示例待处理客户列表:

newList = ["892","24822","23257","9228","8987","8140","9062","10360","8279","917","4565","179084","12384","8851","23107","25250","42550","98059","7279","9404","5454"]

完整Map Reduce脚本代码:

/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
*/
define(['N/record', 'N/log', 'N/runtime', 'N/search'], function (record, log, runtime, search) {
function getInputData(context) {
try{
const input = runtime.getCurrentScript().getParameter({ name: 'custscript_selected_pairs' }) || '[]';
log.audit("input==",input);
let parsedInput = JSON.parse(input);
log.audit("parsedInput=",typeof(parsedInput));
return parsedInput;
}
catch(e)
{
log.error("Error in getInputData",e);
}
}

function map(context) {
log.audit("Inside map");
var data = JSON.parse(context.value);
var itemId = data.itemId;
var customerId = data.customerId;
log.audit("itemId==",itemId);
log.audit("customerId==",customerId);

 try {
     // 获取项目类型
     var itemType = search.lookupFields({
         type: 'item',
         id: itemId,
         columns: ['recordtype']
     }).recordtype;

     log.audit("itemType=",itemType);

     // 加载项目记录
     var itemRec = record.load({
         type: itemType,
         id: itemId
     });

     // 获取多选客户字段值
     var customerField = itemRec.getValue({ fieldId: 'custitemcustomer' });
     log.audit("customerField=",customerField);

     if (customerField) {
         // 按需转换为数组
         var newList = [];

         for (var i = 0; i < customerField.length; i++) {
             if (customerField[i] != customerId) {
                 newList.push(customerField[i]);
             }
         }
         log.audit("newList==",newList);

         // 设置更新后的值并保存
         itemRec.setValue({
             fieldId: 'custitemcustomer',
             value: newList
         });

         // itemRec.save();
         log.audit('Customer removed', 'Removed customer ' + customerId + ' from item ' + itemId);
     }

 } catch (e) {
     log.error('Error in map', 'Item: ' + itemId + ', Customer: ' + customerId + ', Msg: ' + e.message);
 }
}

return { getInputData, map };
});

解决建议

1. 修复类型不匹配问题

NetSuite多选字段getValue返回的是数字数组,而传入的customerId大概率是字符串类型,直接用!=比较会因类型差异导致过滤失效。需统一类型:

// 将customerId转为数字
var targetCustomerId = parseInt(customerId, 10);
// 过滤时使用严格相等判断
if (customerField[i] !== targetCustomerId) {
    newList.push(customerField[i]);
}

2. 启用记录保存

代码中itemRec.save();被注释,这是修改持久化的关键步骤,必须取消注释才能让修改生效。

3. 处理空数组场景

如果过滤后newList为空,需明确设置为null,避免NetSuite忽略空数组的更新请求:

itemRec.setValue({
    fieldId: 'custitemcustomer',
    value: newList.length > 0 ? newList : null
});

4. 验证角色权限

确保脚本部署使用的角色对custitemcustomer字段拥有编辑权限,否则即使代码逻辑正确,也无法保存修改。

5. 增强日志排查

添加类型与值的日志,确认过滤逻辑是否正确执行:

log.audit('Type Check', `Field value type: ${typeof customerField[0]}, Target ID type: ${typeof targetCustomerId}`);

修改后的Map函数示例

function map(context) {
    log.audit("Inside map");
    var data = JSON.parse(context.value);
    var itemId = data.itemId;
    var customerId = data.customerId;
    // 统一类型为数字
    var targetCustomerId = parseInt(customerId, 10);
    
    log.audit("itemId==",itemId);
    log.audit("customerId==",targetCustomerId);

    try {
        var itemType = search.lookupFields({
            type: 'item',
            id: itemId,
            columns: ['recordtype']
        }).recordtype;

        log.audit("itemType=",itemType);

        var itemRec = record.load({
            type: itemType,
            id: itemId
        });

        var customerField = itemRec.getValue({ fieldId: 'custitemcustomer' });
        log.audit("customerField=",customerField);

        let newList = [];
        if (customerField && Array.isArray(customerField)) {
            // 使用filter简化过滤逻辑
            newList = customerField.filter(custId => custId !== targetCustomerId);
            log.audit("newList==",newList);

            itemRec.setValue({
                fieldId: 'custitemcustomer',
                value: newList.length > 0 ? newList : null
            });

            // 保存记录
            var savedRecId = itemRec.save();
            log.audit('Customer removed', `Removed customer ${targetCustomerId} from item ${itemId}, saved record ID: ${savedRecId}`);
        } else {
            log.audit('No customers found', `Item ${itemId} has no customers in custitemcustomer field`);
        }

    } catch (e) {
        log.error('Error in map', `Item: ${itemId}, Customer: ${customerId}, Msg: ${e.message}`);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 18:53:15