【问题标题】:How do I change a line item of a sales order using SuiteScript 2如何使用 SuiteScript 2 更改销售订单的行项目
【发布时间】:2016-09-27 14:17:49
【问题描述】:

好的,这适用于 SuiteScript 1,但由于某种原因,我无法让它与 SuiteScript 2 一起使用。我错过了什么?

客户端(字段更改/在线更改)或服务器端(提交),我无法更改销售订单上的行,例如设置自定义字段。

这些都不起作用。要么找不到函数,要么什么也没发生。我尝试了各种不同的功能和组合来设置文本或值。我只是似乎没有工作:

1.

Rec.setSublistText({
    sublistId : 'item',
    fieldId : 'custcol_example',
    line : i,
    value : "A"
});

2.

Rec.selectLine({
    sublistId : 'item',
    line : i
});
Rec.setCurrentSublistText({
    sublistId : 'item',
    fieldId : 'custcol_example',
    value : "A",
    ignoreFieldChange: true
});
Rec.commitLine();

这在 SuiteScript 1 中完美运行

function clientFieldChanged(type, name, linenum) {
    var Count = nlapiGetLineItemCount("item");
    for (var i = 1; i <= Count; i++) {
        nlapiSelectLineItem("item", i);
        nlapiSetCurrentLineItemValue("item", "custcol_example", "A", false, false);
        nlapiCommitLineItem("item");
    }
}

根据要求,提供更完整的 SS2 示例。没用。

function fieldChanged(scriptContext) {
        var Rec = scriptContext.currentRecord;
        var Count = Rec.getLineCount("item");
        for (var i = 0; i < Count; i++) {
            Rec.selectLine({
                sublistId : 'item',
                line : i
            });
            Rec.setCurrentSublistText({
                sublistId : 'item',
                fieldId : 'custcol_example',
                line : i,
                value : "A"
            });
            Rec.commitLine();
        }
}

【问题讨论】:

    标签: netsuite suitescript


    【解决方案1】:

    您需要在提交该行后保存记录。

    rec.commitLine();
    rec.save();
    

    【讨论】:

    • 我也在尝试相同的方法,但得到以下错误 {"type":"error.SuiteScriptError","name":"RCRD_HAS_BEEN_CHANGED","message":"Record has been changed".. ..有什么帮助吗?
    • 这只是表示您以前保存过此记录,或者此记录已在您正在执行的操作之外保存。我会检查你的代码并检查并确保记录没有保存在其他任何地方。如果找不到任何内容,则可以在编辑之前加载记录,保存记录,然后丢弃对象....根据需要重新加载记录。
    • 是的。我以前将记录保存在某个地方,现在我通过创建不同的对象并保存记录来发现它的工作
    【解决方案2】:

    您的代码 1 中有一个小错误。

    Rec.setSublistText({
        sublistId : 'item',
        fieldId : 'custcol_example',
        line : i,
        value : "A"
    });
    

    由于您使用的是 setSublistText(),因此您应该使用 'text': 'A',而不是 value。它是这样的:

    Rec.setSublistText({
        sublistId : 'item',
        fieldId : 'custcol_example',
        line : i,
        text: "A"
    });
    

    希望这会有所帮助。

    【讨论】:

    • 这很有意义。
    【解决方案3】:

    我在尝试获取联系人的地址记录时遇到了类似的错误。看起来像从子列表子记录中获取数据,您必须使用 record.load 加载记录,并将其置于动态模式。

    不过,我的是 AfterSubmit 函数,因此它可能无法解决您的问题。希望能有所帮助。下面是record.load函数的链接,如果你还没有的话。

    https://netsuite.custhelp.com/app/answers/detail/a_id/45155/kw/record.load

    【讨论】:

      【解决方案4】:

      根据您的 SS1 代码,您应该使用 setCurrentSublistValuesetSublistValue

      Rec.selectLine({
          sublistId : 'item',
          line : i
      });
      Rec.setCurrentSublistValue({
          sublistId : 'item',
          fieldId : 'custcol_example',
          value : "A",
          ignoreFieldChange: true
      });
      Rec.commitLine();
      

      【讨论】:

      • 这是错误:TypeError:在对象 DeferredDynamicRecord 中找不到函数 selectLine。我认为 NS 为我找到了解决方案。我会发布它。
      【解决方案5】:

      您能否展示更多代码并解释这是什么类型的脚本?

      确保您的索引变量是正确的; 1.0 中的子列表行索引从 1 开始,而在 2.0 中它们从 0 开始。

      为了使用selectLine*Current* API,必须在动态模式下加载记录。在客户端脚本中,currentRecord 应始终处于动态模式,但在其他脚本类型中,您需要以动态模式显式加载记录。当您在标准模式而不是动态模式下处理记录时,会发生 无法找到函数 selectLine 错误。

      下面是一个仅在控制台或调试器中运行的 2.0 示例。它以动态模式加载销售订单,然后将所有项目行标记为已关闭。注意itemIndex 是如何从 0 开始的。

      require(["N/record"], function (rec) {
        var salesOrder = rec.load({
          "type": rec.Type.SALES_ORDER,
          "id": 7610,
          "isDynamic": true
        });
      
        closeOrderDynamic(salesOrder);
      
        // Utility function that closes the provided Sales Order record
        // order must be in Dynamic mode
        function closeOrderDynamic(order) {
          var itemIndex = 0;
          var itemCount = order.getLineCount({
            "sublistId": "item"
          });
      
          while (itemIndex < itemCount) {
            order.selectLine({
              "sublistId": "item",
              "line": itemIndex
            });
            order.setCurrentSublistValue({
              "sublistId": "item",
              "fieldId": "isclosed",
              "value": true
            });
            // Must commit the line after we've changed it to save modifications
            order.commitLine({
              "sublistId": "item"
            });
            itemIndex++;
          }
        }
      });
      

      【讨论】:

      • 我添加了一个更完整的 SS2 示例。
      • 您对commitLine 的调用尚未完成。看我的例子;你错过了sublistId 选项:commitLine({sublistId: 'item'});
      • @GordonTruslove:你错过了钥匙——电话是setCurrentSublistValue而不是setCurrentSublistText
      • 此代码在 order.setCurrentSublistValue 调用中为我提供“field.getSublistName 不是函数”
      【解决方案6】:

      您的代码看起来应该适用于这两种情况。您是否尝试过使用 JS 控制台并逐行重复这些脚本?

      例如,在 Chrome 中,开始编辑交易记录,然后点击 Ctrl-Shift-I 打开开发控制台。在那里,您可以逐行复制和粘贴代码,以交互方式查看每个操作的效果。希望到那时,您可以发现什么时候出了问题。

      【讨论】:

      • 我得到了各种各样的错误。最常见的是这样的错误:
      • TypeError: 在对象 DeferredDynamicRecord 中找不到函数 selectLine。
      • 你好像没有正常的记录。您在 GUI 中收到此错误,对吧?
      • 那个来自 NS 错误页面。我使用函数 beforeSubmit(scriptContext) 或函数 fieldChanged(scriptContext) ,我使用的记录对象是 scriptContext.newRecord 或 scriptContext.currentRecord。要么未找到函数,要么未更改值。我将如何在浏览器控制台中测试这些事件?记录值来自事件。
      猜你喜欢
      • 1970-01-01
      • 2021-02-02
      • 2023-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多