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

SuiteScript生成发票时无法将行项目PRICE LEVEL设为CUSTOM

解决方案:脚本创建发票时设置行项目价格层级为CUSTOM

问题根源

你之前的代码报错主要有三个原因:

  1. 字段名错误:价格层级对应的字段是pricelevel,而非price
  2. 数据类型不匹配:CUSTOM价格层级的内部ID是数字-1,你写的'-1' || -1会返回字符串'-1',导致类型校验失败
  3. 行项目操作流程不完整:动态模式下操作子列表必须先选中行,设置后提交行

修正后的代码实现

场景1:从自定义记录afterSubmit触发创建发票

直接在发票创建逻辑中为所有行项目设置CUSTOM价格层级(仅脚本创建的发票会走这个逻辑,不影响UI创建):

function afterSubmit(context) {
    // 仅处理自定义记录的创建事件
    if (context.type !== context.UserEventType.CREATE) return;

    var customRec = context.newRecord;
    // 以动态模式创建发票记录
    var invRecord = record.create({ 
        type: record.Type.INVOICE, 
        isDynamic: true 
    });

    // 设置发票头部必填字段(示例:客户、交易日期)
    invRecord.setValue({ 
        fieldId: 'entity', 
        value: customRec.getValue({ fieldId: 'custrecord_related_customer' }) 
    });
    invRecord.setValue({ 
        fieldId: 'trandate', 
        value: new Date() 
    });

    // 添加并配置行项目
    invRecord.selectNewLine({ sublistId: 'item' });
    // 设置关联商品
    invRecord.setCurrentLineItemValue({ 
        sublistId: 'item', 
        fieldId: 'item', 
        value: customRec.getValue({ fieldId: 'custrecord_related_item' }) 
    });
    // 设置价格层级为CUSTOM(内部ID:-1,必须传数字类型)
    invRecord.setCurrentLineItemValue({
        sublistId: 'item',
        fieldId: 'pricelevel',
        value: -1,
        ignoreFieldChange: true // 避免触发不必要的字段自动计算
    });
    // 手动设置自定义金额(CUSTOM层级必须指定金额)
    invRecord.setCurrentLineItemValue({ 
        sublistId: 'item', 
        fieldId: 'amount', 
        value: 150.00 
    });
    // 提交行项目
    invRecord.commitLine({ sublistId: 'item' });

    // 保存发票
    var invoiceId = invRecord.save({ 
        enableSourcing: true, 
        ignoreMandatoryFields: false 
    });
}

场景2:区分脚本/UI创建的发票(通用方案)

如果需要通过发票自身的事件判断创建来源,可添加一个自定义复选框字段(如custbody_script_generated):

  1. 脚本创建发票时,将该字段设为true
  2. 在发票的beforeSubmit事件中判断:
function beforeSubmit(context) {
    var invRecord = context.newRecord;
    // 仅处理脚本创建的发票
    if (invRecord.getValue({ fieldId: 'custbody_script_generated' }) !== true) return;

    var lineCount = invRecord.getLineCount({ sublistId: 'item' });
    // 循环处理所有行项目
    for (var i = 0; i < lineCount; i++) {
        invRecord.selectLine({ sublistId: 'item', line: i });
        invRecord.setCurrentLineItemValue({
            sublistId: 'item',
            fieldId: 'pricelevel',
            value: -1,
            ignoreFieldChange: true
        });
        // 确保自定义金额已设置
        if (!invRecord.getCurrentLineItemValue({ sublistId: 'item', fieldId: 'amount' })) {
            invRecord.setCurrentLineItemValue({ 
                sublistId: 'item', 
                fieldId: 'amount', 
                value: 0.00 
            });
        }
        invRecord.commitLine({ sublistId: 'item' });
    }
}

关键注意事项

  • 动态模式下操作子列表必须遵循selectLine/selectNewLine → 设置字段 → commitLine的流程
  • 设置pricelevel为-1后,必须手动指定amount字段值,否则NetSuite会自动回退到BASE PRICE
  • 确保脚本拥有发票创建/编辑以及行项目字段修改的权限
  • 使用ignoreFieldChange: true可以避免触发不必要的字段联动计算,减少报错概率

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 17:17:44