Netsuite 脚本:是什么导致 THAT_RECORD_IS_NOT_EDITABLE 错误?

Pat*_*ane 0 javascript oracle netsuite

我正在尝试使用 netsuite 脚本 v2 修改运输订单记录。

 * @NApiVersion 2.0
 * @NScriptType UserEventScript
 */


define(['N/search', 'N/record'],

    function(search, record) {
        function afterSubmit(context) {
            if (context.type !== context.UserEventType.CREATE){
                 return;
            }

            try {
              // Get the current Record
              var salesOrder = context.newRecord;
              salesOrder.setValue({fieldId: 'custbody_route_vendor', value: 1});
              salesOrder.save({enableSourcing: true, ignoreMandatoryFields: true});

            } catch (error) {
              log.error({title: 'Error: ', details: error });
            }

        }

    return {
        afterSubmit: afterSubmit
    };
});
Run Code Online (Sandbox Code Playgroud)

这似乎很简单。但是当我保存销售订单时,它会引发此错误:

{"type":"error.SuiteScriptError","name":"THAT_RECORD_IS_NOT_EDITABLE","message":"该记录不可编辑。","stack":["createError(N/error)...

这不是很有帮助,因为我不知道哪些类型的东西会使记录无法编辑。

我搜索了文档和互联网,但找不到此错误的参考。该保存功能的文档没有解决可能的错误:

https://system.na2.netsuite.com/app/help/helpcenter.nl?fid=section_4267286323.html

任何建议都会非常有帮助。

Kry*_*ton 5

context传递给afterSubmit函数是只读的。您不能修改然后保存新创建的记录。您需要:

  1. 请改用该beforeSubmit函数。UsingbeforeSubmit允许您在将记录提交到数据库之前更改记录 - 但是,您不应调用Record.save()它,因为系统将在您beforeSubmit完成后保存修改后的记录。
  2. 您也可以使用该afterSubmit功能,但为此您需要单独加载记录,根据需要进行修改,然后保存。