NetSuite SuiteScript设置缺货行为报错:无法读取DISABLE属性求助
解决SuiteScript中TypeError: Cannot read property "DISABLE" from undefined错误
错误原因
这个错误表明你在代码里尝试访问一个未定义(undefined)对象的DISABLE属性。在NetSuite SuiteScript场景下,最常见的触发原因是未正确引用缺货行为(outofstockbehavior)对应的官方枚举对象,或是枚举模块未被正确加载。
修复方案
1. 正确引用NetSuite官方枚举(SuiteScript 2.x)
首先确保导入N/record模块,然后通过record.InventoryItemOutofStockBehavior访问对应的枚举值,不要自行定义非官方常量:
define(['N/record'], function(record) { function setOutOfStockBehavior() { // 测试目标商品ID const targetItemId = 10336; // 加载商品记录 const itemRecord = record.load({ type: record.Type.INVENTORY_ITEM, id: targetItemId }); // 获取可用库存 const qtyAvailable = itemRecord.getValue({fieldId: 'quantityavailable'}); let outOfStockBehavior; // 根据库存设置对应缺货行为 if (qtyAvailable === 0) { // 对应"Disallow back orders but display out-of-stock message" outOfStockBehavior = record.InventoryItemOutofStockBehavior.DISALLOW_BACK_ORDERS_SHOW_MESSAGE; } else { // 对应"Allow back orders with no out-of-stock message" outOfStockBehavior = record.InventoryItemOutofStockBehavior.ALLOW_BACK_ORDERS_HIDE_MESSAGE; } // 赋值并保存记录 itemRecord.setValue({ fieldId: 'outofstockbehavior', value: outOfStockBehavior }); itemRecord.save(); } return { execute: setOutOfStockBehavior }; });
2. 关键检查点
- 确认
N/record模块已正确导入,无拼写错误,脚本部署时的模块引用配置正常 - 不要使用自定义的
DISABLE类常量,必须用NetSuite官方提供的枚举值:- 禁止缺货订单但显示缺货提示:
DISALLOW_BACK_ORDERS_SHOW_MESSAGE - 允许缺货订单且不显示提示:
ALLOW_BACK_ORDERS_HIDE_MESSAGE
- 禁止缺货订单但显示缺货提示:
- 调试时可先打印枚举对象,确认其已加载:
log.debug('枚举对象', record.InventoryItemOutofStockBehavior);
3. 针对SuiteScript 1.x的补充(若使用旧版本)
如果是SuiteScript 1.x,缺货行为的取值为字符串常量,无需枚举对象:
function setOutOfStockBehavior() { const targetItemId = 10336; const itemRecord = nlapiLoadRecord('inventoryitem', targetItemId); const qtyAvailable = itemRecord.getFieldValue('quantityavailable'); let outOfStockBehavior; if (qtyAvailable === '0') { outOfStockBehavior = 'DISALLOW_BACK_ORDERS_SHOW_MESSAGE'; } else { outOfStockBehavior = 'ALLOW_BACK_ORDERS_HIDE_MESSAGE'; } itemRecord.setFieldValue('outofstockbehavior', outOfStockBehavior); nlapiSubmitRecord(itemRecord); }
内容的提问来源于stack exchange,提问作者Sunny R
相关产品推荐
相关产品推荐

