NetSuite RESTlet保存的搜索返回限制

时间:2017-04-20 22:27:23

标签: json rest netsuite restlet

我使用以下JSON RESTlet脚本来导出一些数据。由于这些限制,它的上限为1000行,远低于我需要出口的总量。我遇到了一些不同的解决方案,但JSON / RESTlet对我来说还是一个新手,所以我正在寻找一些关于如何调整代码以循环遍历所有结果的反馈。

function GetSearchResult(){
    //array container for search results
    var output = new Array();

    //get search results
    var results = nlapiSearchRecord('transaction','customsearchid',null,null);
    var columns = results[0].getAllColumns();

    //loop through the search results
    for(var i in results){
        //create placeholder object place holder
        var obj = new searchRow(
          //set the values of the object with the values of the appropriate columns
          results[i].getValue(columns[0]),
          results[i].getValue(columns[1]),
          results[i].getValue(columns[2]),
          results[i].getValue(columns[3]),
          results[i].getValue(columns[4]),
          results[i].getValue(columns[5]),
          results[i].getValue(columns[6]),
          results[i].getValue(columns[7]),
          results[i].getValue(columns[8]),
          results[i].getValue(columns[9]),
          results[i].getValue(columns[10]),
          results[i].getValue(columns[11]),
          results[i].getValue(columns[12])
          );

        //add the object to the array of results
        output.push(obj);
    }

    //return the array of search objects
    return output;
}

//Object to serve a place holder for each search row
function searchRow(internalid,lineid,subsidiaryid,locationid,departmentid,accountid,date,name,memo,amount,uniqueid,product,period){
    this.internalid = internalid;
    this.lineid = lineid;
    this.subsidiaryid = subsidiaryid;
    this.locationid = locationid;
    this.departmentid = departmentid;
    this.accountid = accountid;
    this.date = date;
    this.name = name;
    this.memo = memo;
    this.amount = amount;
    this.uniqueid = uniqueid;
    this.product = product;
    this.period = period;

}

这是我试图遵循的一个例子,无济于事:

var types = ["Estimate","Opprtnty","SalesOrd","PurchOrd","CustInvc","CashSale"];
var filters = new Array(); //define filters of the search
filters[0] = new nlobjSearchFilter('type',null,'anyof',types);
filters[1] = new nlobjSearchFilter('mainline',null,'is','T');
var columns = new Array();
columns[0] = new nlobjSearchColumn('internalid').setSort(); //include internal id in the returned columns and sort for reference
var results = nlapiSearchRecord('transaction',null,filters,columns); //perform search
var completeResultSet = results; //container of the complete result set
while(results.length == 1000){ //re-run the search if limit has been reached
     var lastId = results[999].getValue('internalid'); //note the last record retrieved
     filters[2] = new nlobjSearchFilter('internalidnumber',null,'greaterthan',lastId); //create new filter to restrict the next search based on the last record returned
     results = nlapiSearchRecord('transaction',null,filters,columns);
     completeResultSet = completeResultSet.concat(results); //add the result to the complete result set 
} 

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

这是一个通用函数,可以加载Netsuite保存的搜索,并将结果检索为标准Javascript对象的数组,并且不会限制为1000个结果限制。

function getSearchResults(id) {
  var search = nlapiLoadSearch(null, id);
  var columns = search.getColumns();
  var resultSet = search.runSearch();

  var results = [];
  var slice = [];
  var i = 0;

  do {
    slice = resultSet.getResults(i, i + 1000);
    slice.forEach(function(result) {
      var resultObj = {};
      columns.forEach(function(column) {
        resultObj[column.getName()] = result.getValue(column);   
      });
      results.push(resultObj);
      i++;
    });
  } while (slice.length >= 1000);

  return results;
}

答案 1 :(得分:1)

这是同一脚本的restlets 2.0版本。要将其添加到Netsuite:

  • 已启用检查脚本:选择设置>公司>设置任务>启用功能。单击SuiteCloud子选项卡。确保选中Client和Server SuiteScript。
  • 文档。从左侧的文件夹(例如Suitescripts)中选择一个位置,然后单击“添加文件”。
  • 编辑以下内容并将其另存为mySavedSearch.js,然后从上面的网站上传。
  • 自定义>脚本>脚本>新建。选择mySavedSearch.js。填写名称和ID字段,保存。
  • 单击“部署脚本”。将状态从“测试”更改为“已发布”,并适当设置权限。这将为您提供一个URL和一个外部URL(类似:https://[your-ID].restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=1234&deploy=1)。
  • 设置Netsuite邮递员收藏集:(在此处转到Where is the Netsuite Webservices Postman Collection?),将其配置为发送正确的auth标头。对从上一步获得的URL执行获取请求,您将获得保存的搜索的JSON。

    /**
     * @NApiVersion 2.x
     * @NScriptType Restlet
     * @NModuleScope SameAccount
     */
    define(['N/search','N/record'],
    
    function(search,record) {
    
        /**
         * Function called upon sending a GET request to the RESTlet.
         *
         * @param {Object} requestParams - Parameters from HTTP request URL; parameters will be passed into function as an Object (for all supported content types)
         * @returns {string | Object} HTTP response body; return string when request Content-Type is 'text/plain'; return Object when request Content-Type is 'application/json'
         * @since 2015.1
         */
        function doGet(requestParams) {
    
            var results = [];
            var slice = [];
            var i = 0;
    
            var mySearch = search.load({
                id: 'customsearch12345' // change to the ID of your saved search
            });
    
            var resultSet = mySearch.run();
    
            do {
                slice = resultSet.getRange({ start: i, end: i + 999 });
                slice.forEach(function(row) {
                    var resultObj = {};
                    row.columns.forEach(function(column) {
                        resultObj[column.name] = row.getValue(column);
                        });
                    results.push(resultObj);
                    i++;
                });
            } while (slice.length >= 1000);
    
            return JSON.stringify(results);
        }
    
        return {
            'get': doGet,
        };
    
    });