首页 > 解决方案 > NetSuite SuiteScript 2.0 如何在 N/search create API 中指定字段、子列表字段或子记录字段

问题描述

我在采购订单记录上有一个按钮,它对当前记录执行保存的搜索查询,然后使用 http 模块通过 POST 将该数据发送到 url。然后,该 url 将作为成功确认的一部分发回的数据发送回去。保存搜索的想法是创建一个 javascript 对象,其中包含我想要的采购订单中的所有数据(主记录和带有子记录的项目子列表),然后JSON.stringify用于为 http POST 创建 JSON 有效负载。我不能这样做,currentRecord因为如果你检查它,它只包含内部 id。这也使我不必费力地编写大量代码来手动从currentRecord.

不幸的是,我真的不明白如何在动态创建的保存搜索中指定列名。有时在我看来,列名是来自 NetSuite 记录浏览器的列名,有时如果我使用来自 NetSuite 记录浏览器的列名(例如 ),脚本会给出错误(未找到列currencysymbol)。

我也不确定如何指定出现在子列表中的列或子列表中的子记录。我尝试使用item.itemtype,但这给了我一个未找到列的错误。刚刚item成功完成,但我不确定这是否真的成功,因为很难解码返回的结果JSON.stringify(它添加了很多反斜杠)。使用console.log(由于某种原因,我使用 NetSuite 没有得到任何回报log.audit)也非常困难,尽管它看起来返回一个包含 5 行的数组。所以使用item可能会成功。我之所以这么说是因为我有 3 个项目行并且它返回 5 个数组行。

所以基本上我想知道在哪里可以找到在 NetSuite 中用于保存搜索的列名称;以及如何在保存的搜索中指定子列表列名和子列表子记录列名。

/**
 * @NApiVersion 2.0
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/ui/dialog', 'N/currentRecord', 'N/record', 'N/url', 'N/http', 'N/search'], function (dialog, rec, record, url, http, s) {

   function pageInit(context) {
      // All client scripts need at least one dummy function.
   }   // pageInit

   function onButtonClick() {

      var currentRecord = rec.get();

      // Create a saved search dynamically.
      var rs = s.create({
         type: s.Type.PURCHASE_ORDER,
         filters: [
            ["mainline", s.Operator.IS, "F"], "and",
            ["internalid", s.Operator.IS, currentRecord.id]
         ],
         columns: [
            "internalid",
            "currency",
            {
                name: "item",
                sort: s.Sort.ASC   // DESC
            }
         ]
      });

      var myPostDataObj = rs.run().getRange(0, 1000);
      console.log(myPostDataObj);
      var headers = {
         'Content-Type': 'application/json; charset=utf-8',
      };
      http.post.promise({
         url: 'http://httpbin.org:80/post',
         body: JSON.stringify(myPostDataObj),
         headers: headers
      })
      .then(function(response){
         console.log('SUCCESS: ' + JSON.stringify(response));
      })
      .catch(function onRejected(reason) {
         console.log('ERROR: ' + JSON.stringify(reason));
      })

   }

   return {
      pageInit: pageInit,
      onButtonClick: onButtonClick
   };

});   // Define

标签: netsuitesuitescript2.0

解决方案


我建议您购买两个 Chrome 扩展程序;

  1. NetSuite 字段资源管理器。此扩展将向您显示 NetSuite 记录中所有字段的 ID(和值)。
  2. NetSuite 搜索导出。此扩展程序会将保存的搜索转换/导出到 SuiteScript。

对于您正在做的事情,我将在 UI 中创建您保存的搜索,然后使用 Saved Search Export 扩展程序将其导出,将其粘贴到您的代码中(在哪里s.create),然后从那里开始工作。

第一个扩展很适合获取子列表字段 ID。在记录浏览器中保存查找。


推荐阅读