NetSuite RESTlet 保存的搜索返回限制

Rya*_*yan 2 rest json netsuite

我使用以下 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;

}
Run Code Online (Sandbox Code Playgroud)

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

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 
} 
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助!

Sil*_*mer 5

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

  • 检查脚本是否已启用:选择“设置”>“公司”>“设置任务”>“启用功能”。单击 SuiteCloud 子选项卡。确保客户端和服务器 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 postman 集合后:(此处的操作方法:Netsuite Webservices Postman 集合在哪里?),它将被配置为发送正确的身份验证标头。对您在上一步中获取的 URL 执行 get 请求,您将获得已保存搜索的 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 + 1000 });
                 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,
         };
    
     });
    
    Run Code Online (Sandbox Code Playgroud)