"No more than 4000 search results may be returned ...
# ask-stanley-ai
n
"No more than 4000 search results may be returned at one time for this function function initAverageCost() { var myArray = []; var mySearch = search.load({ id: 755, type: search.Type.ITEM }); mySearch.run().each(function(result) { var resultObj = {}; for (var i = 0; i < result.columns.length; i++) { var column = result.columns[i]; var columnKey = column.name; resultObj[columnKey] = result.getValue(column); } myArray.push(resultObj); return true; }); log.debug({ title: "myArray ", details: myArray }); return myArray; }
c
AI generated answer (using internal Slack threads):
The 4000 results limit is a restriction imposed by NetSuite for the
.each()
method. However, there are ways to work around this limitation. One way is to use the
getRange()
method instead of
.each()
. The
getRange()
method allows you to specify a range of results to return, so you can use it in a loop to retrieve all results in chunks. Here is an example:
Copy code
javascript
function searchAll(resultsetCurrent) {
    var allResults = [];
    var startIndex = 0;
    var RANGECOUNT = 1000;

    do {
        var pagedResults = resultsetCurrent.getRange({
            start: parseInt(startIndex),
            end: parseInt(startIndex + RANGECOUNT),
        });

        allResults = allResults.concat(pagedResults);
        var pagedResultsCount = pagedResults != null ? pagedResults.length : 0;
        startIndex += pagedResultsCount;

        var remainingUsage = runtime.getCurrentScript().getRemainingUsage();
        log.debug(remainingUsage);
    } while (pagedResultsCount == RANGECOUNT);
    var remainingUsage = runtime.getCurrentScript().getRemainingUsage();
    return allResults;
}
Another approach is to use a library like NFT (NetSuite Fast Track) which provides a lazy processing model. This allows you to process all results until either completion or governance is exhausted. Here is an example:
Copy code
javascript
Seq(LazySearch.load(searchid))
    .takeWhile(governanceRemains())
    .map(nsSearchResult2obj())
    .forEach(r => loadAndCloseRMA(r))
In this example, RMA ids are taken from a saved search and 'closed'. The processing is 'lazy' in the sense that no matter how many steps you chain together, no intermediate collections are created. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.850546479
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.840056658
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.838746369
Please provide your feedback on this answer.