NetSuite库存调整Before Record Load脚本从销售订单带入行项目报错求助
库存调整行写入失效问题修复方案
报错本质
系统返回"You must enter at least one line item for this transaction"的核心原因是:代码写入库存调整记录inventory子列表的操作全部未生效,最终提交的记录子列表无有效行。
逐点问题定位与修复
1. 子列表行写入缺少NetSuite强制流程
交易类记录的动态子列表(如库存调整的inventory子列表)不支持直接通过指定行号写入值,必须逐行执行「激活新空白行→逐字段赋值→提交当前行」的标准流程,否则所有setSublistValue操作不会同步到记录的内存结构中,保存时系统会判定子列表无内容。
正确的单行写入流程:
- 遍历到符合筛选条件的商品时,先调用
newRecord.selectNewLine({sublistId: 'inventory'})激活新的空白行 - 逐字段为当前激活行设置字段值
- 所有字段赋值完成后调用
newRecord.commitLine({sublistId: 'inventory'})将该行正式加入子列表缓存
2. 商品类型判断逻辑完全失效,循环从未进入有效分支
当前用于读取商品类型的代码存在API格式错误,导致取值永远为空:
- 代码
cfrom_rec.getSublistValue('item','itemtype',x)使用SuiteScript 1.0风格的位置参数传值,SuiteScript 2.0N/record模块的getSublistValue方法仅支持对象格式参数,该调用会直接返回undefined,导致item_type == 'InvtPart'的判断永远不成立,从未执行过实际的行写入操作。 - 修正为与其他取值逻辑一致的参数格式:
var item_type = cfrom_rec.getSublistValue({ sublistId: 'item', fieldId: 'itemtype', line: x });
3. 调整数量的空值判断存在类型错误
代码对读取到的原始数量值做了parseFloat转换,当源字段值为空时,转换结果为NaN而非空字符串'',原判断if(adjustqty == '')永远不会触发,最终会将NaN写入调整数量字段,触发字段校验失败。
修正逻辑为:
var rawQty = cfrom_rec.getSublistValue({ sublistId: 'item', fieldId: productionquantity, line: x }); var adjustqty = parseFloat(rawQty); if(isNaN(adjustqty)){ adjustqty = 0; }
修正后的核心循环代码
var cfrom_rec = record.load({type: cfrom_type, id: cfrom_id}); var ia_location = cfrom_rec.getValue('location'); var productionquantity = 'custcol_prod_quantity_1_entre'; var cfrom_line = cfrom_rec.getLineCount('item'); for(var x=0; x<cfrom_line; x++){ var item_type = cfrom_rec.getSublistValue({ sublistId: 'item', fieldId: 'itemtype', line: x }); log.debug('item_type', item_type); if(item_type === 'InvtPart'){ // 激活新的库存调整子列表空白行 newRecord.selectNewLine({sublistId: 'inventory'}); // 逐字段写入值 var item_id = cfrom_rec.getSublistValue({sublistId: 'item', fieldId: 'item', line: x}); newRecord.setSublistValue({sublistId: 'inventory', fieldId: 'item', value: item_id}); newRecord.setSublistValue({sublistId: 'inventory', fieldId: 'location', value: ia_location}); var rawQty = cfrom_rec.getSublistValue({sublistId: 'item', fieldId: productionquantity, line: x}); var adjustqty = parseFloat(rawQty); if(isNaN(adjustqty)) adjustqty = 0; newRecord.setSublistValue({sublistId: 'inventory', fieldId: 'adjustqtyby', value: adjustqty}); newRecord.setSublistValue({sublistId: 'inventory', fieldId: 'units', value: cfrom_rec.getSublistValue({sublistId: 'item', fieldId: 'units', line: x})}); newRecord.setSublistValue({sublistId: 'inventory', fieldId: 'custcol_hnd_skuline', value: cfrom_rec.getSublistValue({sublistId: 'item', fieldId: 'custcol_hnd_skuline', line: x})}); // 提交当前激活行,写入子列表 newRecord.commitLine({sublistId: 'inventory'}); } }
优化建议
- 遍历完成后增加判断逻辑:如果有效行数量为0,主动弹出提示告知用户来源销售订单没有符合条件的可调整商品,避免触发无意义的系统报错
- 确认Before Record Load脚本的绑定场景包含
create模式,避免在编辑已有记录时重复追加行导致数据异常 - 可在循环内增加日志,记录每次读取的item_type值,便于排查类型不匹配导致的筛选失效问题
内容的提问来源于stack exchange,提问作者Saizou
相关产品推荐
相关产品推荐

