簡體   English   中英

嘗試從自定義記錄字段為客戶設置自定義字段值

[英]Trying to Set a Custom Field Value on a Customer, from a Custom Record Field

我正在為一個應該執行以下操作的腳本而苦苦掙扎:

  1. 搜索客戶的列表
  2. 獲取幾個Field Values,其中一個用於下一次Search,另一個是ID
  3. 搜索自定義記錄列表,條件是我剛剛獲取的字段之一
  4. 獲取字段值
  5. 並使用之前獲取的客戶 ID 將自定義記錄字段值分配給客戶上的自定義字段。

但它在第二次搜索中退出,說由於無效的搜索條件而返回“未定義”。 我假設我從第一次搜索中獲得的字段在第二次搜索的標准中不起作用?

我的代碼在下面 - 它是一個明顯的代碼(像往常一樣),還是 go 的錯誤方式?

/**
 *@NApiVersion 2.x
 *@NScriptType ScheduledScript
 */

define(['N/search'],

function getShipSuburbId(search) {
  function execute() {
    var customerSearchObj = search.create({
        type: "customer",
        filters:
            [
                ["custentity_store_shipping_suburb","isnotempty",""]
            ],
        columns:
            [
                search.createColumn({
                    name: "entityid"
                }),
                search.createColumn({name: "custentity_store_shipping_suburb"})
            ]
    });
    var custSearchResult = customerSearchObj.runPaged({pageSize: 1000});
    log.debug({title: "customerSearchObj result count", details: custSearchResult.count});
    var custNumPages = custSearchResult.pageRanges.length;
    var custAllResults = [];
    var i = 0;
    while (i < custNumPages) {
        custAllResults = custAllResults.concat(custSearchResult.fetch(i).data);
        i++;
    }

        return custAllResults;
        for (var j = 0; j < custAllResults.length; j++) {
            var currentRecord = custAllResults[j].getValue({
                name: "entityid"
            });
            var shipSub = custAllResults[j].getValue({
                name: "custentity_store_shipping_suburb"
            });
};

var shipSubIdSearch = search.create({
   type: "customrecord_suburb",
   filters:
   [
      ["name","is",shipSub]
   ],
   columns:
   [
      search.createColumn({
         name: "internalid",
         summary: "MAX",
         label: "Internal ID"
      })
   ]
});

        var allSubIdResults = shipSubIdSearch.runPaged({pageSize: 1});
    log.debug({title: "shipSubIdSearch result count", details: allSubIdResults.count});
    var subNumPages = custSearchResult.pageRanges.length;
    var subAllResults = [];
    var m = 0;
    while (m < subNumPages) {
        subAllResults = subAllResults.concat(allSubIdResults.fetch(m).data);
        m++;
    }
          return subAllResults;
          
    for (var k = 0; k < subAllResults.length; k++) {
    var shipSubId = subAllResults[k].getValue({
        name: "internalid"
    });
};
      var setSuburbId = currentRecord.setValue({

                    fieldId: 'custentity_shipping_suburb_id',

                    value: shipSubId

                });
      return setSuburbId;
  }
  return {
    execute : execute
  };

});

下面的新代碼

/**
 *@NApiVersion 2.x
 *@NScriptType ScheduledScript
 */

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

function getShipSuburbId(search, record) {
  function execute() {

      var customerSearchObj = search.create({
          type: "customer",
          filters:
              [
                  ["custentity_store_shipping_suburb", "isnotempty", ""]
              ],
          columns:
              [
                  search.createColumn({
                      name: "entityid"
                  }),
                  search.createColumn({name: "custentity_store_shipping_suburb"})
              ]
      });     // The first search, which draws a list of Customers

      var custSearchResult = customerSearchObj.runPaged({pageSize: 1000});    // Run paged
      log.debug({title: "customerSearchObj result count", details: custSearchResult.count});
      var custNumPages = custSearchResult.pageRanges.length;

      var custAllResults = [];
      var i = 0;
      while (i < custNumPages) {
          custAllResults = custAllResults.concat(custSearchResult.fetch(i).data);
          i++;
      }

      for (var j = 0; j < custAllResults.length; j++) {
          var currentRecord = custAllResults[j].getValue({
              name: "entityid"
          });
          var shipSub = custAllResults[j].getValue({
              name: "custentity_store_shipping_suburb"
          });
          log.debug({title: "currentRecord", details: currentRecord});
          log.debug({title: "shipSub", details: shipSub});
          // I've left this "for" operation open for the next search - possible issue?


          var shipSubIdSearch = search.create({
              type: "customrecord_suburb",
              filters:
                  [
                      ["name", "is", shipSub]
                  ],
              columns:
                  [
                      search.createColumn({
                          name: "internalid",
                          summary: "MAX",
                          label: "Internal ID"
                      })
                  ]
          }); // Second search. This should only return one result each time it is run
          var subIdRun = shipSubIdSearch.run();
          log.debug({title: "subIdRun result count", details: subIdRun.count});

          var shipSubId = subIdRun.each(
            function (result) {
              log.debug({
                  title: "Fetch ID",
                  details: result.getValue({name: "internalid"})
              })
              return true;
          });
          log.debug({title: "shipSubId result", details: shipSubId});

          var myRecord = record.load({
              type: 'customer',
              id: currentRecord
          }); // Load the Customer record, based on the id fetched in the first search
          log.debug({title: "myRecord", details: myRecord});

          myRecord.setValue({

              fieldId: 'custentity_shipping_suburb_id',

              value: shipSubId

          }); // And set the value of the Custom field, based on value from second search

      }
  }
  return {
    execute : execute
  };

});

以及新腳本上的執行日志截圖: 執行日志截圖

我試圖編輯您的一些代碼以使您更接近答案。 我留下了評論,解釋了您需要采取的步驟。

試試這個,讓我知道它是怎么回事!

/**
 *@NApiVersion 2.1
 *@NScriptType ScheduledScript
 */

define(['N/search', "N/record"], function (search, record) {
    function execute() {
        let customers = [];
        let storeShippingSuburbIds = [];
        let storeShippingSuburbId;

        let searchCustomers = search.create({
            type: "customer",
            filters:
                [
                    ["custentity_store_shipping_suburb", "isnotempty", ""]
                ],
            columns:
                [
                    search.createColumn({name: "entityid"}),
                    search.createColumn({name: "custentity_store_shipping_suburb"})
                ]
        });

        var pagedData = searchCustomers.runPaged({pageSize: 1000});

        pagedData.pageRanges.forEach(function (pageRange) {
            let page = pagedData.fetch({index: pageRange.index});

            page.data.forEach(function (result) {
                customers.push([
                    result.getValue({name: "entityid"}),
                    result.getValue({name: "custentity_store_shipping_suburb"})
                ])

                storeShippingSuburbIds.push(result.getValue({name: "custentity_store_shipping_suburb"}));
                return true;
            });
        });

        /*
         * I think you want the search operator of anyof here.
         */
        search.create({
            type: "customrecord_suburb",
            filters:
                [
                    ["name", "anyof", storeShippingSuburbIds]
                ],
            columns:
                [
                    search.createColumn({
                        name: "internalid",
                        summary: "MAX",
                        label: "Internal ID"
                    })
                ]
        }).run().each(function (result) {
            storeShippingSuburbId = result.getValue(result.columns[0]);
        });

        /*
         * You'll need to use record.load() here or record.submitFields(), depending on a few things.
         * But, it won't work as it is because it doesn't know what the "current record" is.
         */
        let myRecord = record.load();

        myRecord.setValue({
            fieldId: 'custentity_shipping_suburb_id',
            value: storeShippingSuburbId
        });

        myRecord.save();
    }

    return {
        execute: execute
    };

});

我發現處理多級搜索的最佳方法是將每次搜索的結果收集到一個數組中。
一旦我處理完一次搜索的結果,然后一個一個地處理數組的每個元素,無論是搜索、查詢、編輯等。

這樣做的好處包括:

  1. 如果是計划腳本,這允許我在第一次搜索后檢查治理使用情況,並在必要時將結果數組作為參數重新計划。
  2. 在繼續其他操作之前處理其他邏輯,如數組元素上的排序或公式。
  3. 處理速度可能更快,特別是如果您不需要對第一次搜索的某些結果進行任何進一步的操作。
  4. 這只是個人喜好,但它更容易將腳本的元素拆分為單獨的函數並保持可讀性和邏輯順序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM