NetSuite SuiteScript 1.0报错'The record is not a valid object'求助
编写了NetSuite SuiteScript 1.0脚本,尝试更新已保存搜索返回的所有记录字段,但始终收到错误提示:The record is not a valid object。加载记录的代码为var item = nlapiLoadRecord(res.getRecordType(), intid);,其中intid是物料记录的内部ID(已通过调试日志确认正确性),尝试硬编码记录类型如inventoryitem或assemblyitem后仍出现相同错误。
完整代码如下:
function SyncBCID(type) { var search = nlapiLoadSearch('item','customsearch_script_itm_meta_fields'); var results = search.runSearch(); //nlapiLogExecution('DEBUG', '-- Start --'); results.forEachResult(function(res){ var intid = res.getValue('internalid'); var bcid = res.getValue('custrecord_celigo_bg_siim_productid', "custrecord_celigo_bg_siim_nsid"); var sku = res.getValue('custitem_amazon_product_code'); var rectype = res.getRecordType(); nlapiLogExecution('DEBUG', 'BC ID: '+bcid+' SKU: '+sku+' Internal ID: '+intid+' Record Type: '+rectype); try { var item = nlapiLoadRecord(res.getRecordType(), intid); item.setFieldValue('custitem_bc_int_id', bcid, false); nlapiSubmitRecord(res.getRecordType(), true); } catch(e) { var errMsg = e.getDetails(); if (errMsg != 'Danger Will Robinson') { nlapiSendEmail(17800280, 'netsuite@xxxxxxx.com', 'Error with updating BC ID', 'Error updating BC ID for '+sku+'. Error: '+errMsg); nlapiLogExecution('ERROR', e.getCode(), errMsg); } } return true; }); //nlapiLogExecution('DEBUG', '-- End --'); }
问题排查与修正方向
核心错误:nlapiSubmitRecord参数错误
代码中nlapiSubmitRecord(res.getRecordType(), true);的调用完全错误,该函数第一个参数要求传入加载后的记录对象,而非记录类型字符串。正确写法应为:nlapiSubmitRecord(item, true);这是导致
The record is not a valid object报错的直接原因。验证搜索返回的记录类型合法性
虽然调试日志打印了记录类型,但需确认其是否为NetSuite支持的可编辑物料类型,比如inventoryitem、assemblyitem、noninventoryitem等,避免搜索结果混入无法编辑的特殊记录类型。检查权限配置
确保脚本部署的权限集允许操作目标记录类型,同时当前执行脚本的角色拥有编辑物料记录及自定义字段custitem_bc_int_id的权限。性能优化方案
若仅需更新单个字段,无需加载整条记录,可直接使用nlapiSubmitField提升效率,示例代码:// 替换原try块内的代码 try { nlapiSubmitField(res.getRecordType(), intid, 'custitem_bc_int_id', bcid); } catch(e) { // 异常处理逻辑不变 }
内容的提问来源于stack exchange,提问作者John

