Reputation: 1784
Is there a way to limit the amount of records returned via SuiteScript? I use the following method, but the search still gets all results. I am breaking out of the iteration of the results if I've hit the max I'd like to return. I can't find anything in the documentation.
In the UI it looks like one can limit the returned results, even though I haven't had much luck with it. (Maybe because I'm joining...)
var accountSearch = search.create({
type: search.Type.CUSTOMER,
columns: searchColumns,
filters: searchFilters
});
var searchResultsPagedData = accountSearch.runPaged({
'pageSize': 1000
});
var max = 9;
for (var pageIndex = 0; pageIndex < searchResultsPagedData.pageRanges.length; pageIndex++) {
var pageRange = searchResultsPagedData.pageRanges[pageIndex];
if (pageRange.index >= max)
break;
var searchPage = searchResultsPagedData.fetch({ index: pageRange.index });
// Iterate over the list of results on the current page
searchPage.data.forEach(function (result) {
}
}
Upvotes: 2
Views: 3771
Reputation: 5231
Another approach: from the comments it seems one of the issues you're having is running into the execution limit, so you could load the N/runtime
module and use Script.getRemainingUsage() before each fetch to ensure you have enough governance units left - and break or return if you don't.
Upvotes: 1
Reputation: 8847
Use getRange
instead of each
to iterate over your results. getRange
lets you specify a start
and end
index to grab a specific slice of the results.
Upvotes: 4
Reputation: 5231
Why not just set the page size to the max number of results you want and only iterate over the first page (page 0)? Or if you're looking for more than 1000 results you could limit the number of pages by setting pageIndex < max;
instead of pageIndex < searchResultsPagedData.pageRanges.length;
Upvotes: 4