Friday, November 9, 2018

Retrieving search results that exceed the API row limit in SuiteScript

To retrieve search results that exceed the 1000 row limit returned by nlapiSearchRecord, use the following code snippet as guide:

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

No comments:

Post a Comment