2

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...)

NS Seach Max Results

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) {
    }
}
ehcanadian
  • 1,738
  • 1
  • 15
  • 23

3 Answers3

4

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;

Krypton
  • 4,394
  • 2
  • 9
  • 13
  • I'll almost always be looking for more than 1000 rows as I limit the result to 10,000. In some cases the search result could be hundreds of thousands of rows. I'm trying to avoid getting them in the first place. – ehcanadian Jun 09 '17 at 16:57
  • What is the point of doing the search if you don't want all the rows? What are you trying to achieve? e.g. would you be better off using aggregate functions to provide a narrowing of the range? If you are breaking on max pages all you would save by breaking out of the `searchPage.data.forEach` would be one page's worth of search. What do you hope to gain? – bknights Jun 09 '17 at 19:08
  • I am probably better off using aggregate functions if I can figure it out. I don't need all the rows because the results will be shown to the user in a graphical way. (Pins on map.) No one needs to see 200K pins on a map. I am breaking because if the result set has 30 pages and I don't need them, there is no sense in fetching those results. NetSuite has an execution limit and for some reason I'm hitting it. It's not related to this question (I think) but I figured I would cover all my bases. – ehcanadian Jun 09 '17 at 19:41
  • NetSuite does not return the results until you `fetch` them with `PagedData.fetch`. `Search.runPaged` only returns summary information about the paginated results. Hence if you only run the `.fetch` method on the first x pages you should only be working with the the first x * pageSize results. – Krypton Jun 09 '17 at 20:14
  • @Krypton thanks for confirming that. I have no idea why my script is timing out, but like I mentioned, I want to make sure I am not missing anything. I guess this isn't a very worthy question. I mean, it's only 10K records... – ehcanadian Jun 09 '17 at 20:28
4

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.

erictgrubaugh
  • 8,519
  • 1
  • 20
  • 28
  • 1
    Why though? What is the difference and what is the benefit? Thanks for the reply, I am honestly curious. – ehcanadian Jun 10 '17 at 00:47
  • The difference is just a convenience API and the task you're trying to accomplish. If you want a very specific slice of your results, `getRange` is a nice concise way of doing so; if you want to iterate over your results one at a time from the beginning, then `each` is there. – erictgrubaugh Jun 10 '17 at 02:35
  • 1
    After doing quite a bit of testing, `getRange` is slower than using `runPaged` and using each. `runPaged` has an initial hit, but I found `getRange` to run as long as each `runPaged` did. – ehcanadian Jun 14 '17 at 17:55
  • How much slower? – erictgrubaugh Jun 14 '17 at 21:22
  • Each call to `getRange` is equivalent to one call of `runPaged`. So for 10,000 records, 10X as slow. (Calling `getRange` 10 times.) Calling fetch is much, much quicker. – ehcanadian Jun 16 '17 at 00:29
  • Would you be willing to share your test harness? – erictgrubaugh Jun 16 '17 at 00:32
1

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.

Krypton
  • 4,394
  • 2
  • 9
  • 13
  • I should probably do that, thanks. I'm doing this in a restlet, so I didn't think I would hit a governance issue. I don't think I am actually, since I am getting SSS_TIME_LIMIT_EXCEEDED. – ehcanadian Jun 12 '17 at 00:01