Friday, November 23, 2018

Get the total length of an nlobjSearchResultSet object

 

In SuiteScript, there are two ways to perform record searches - by calling the nlapiSearchRecord API, and by using the runSearch function of the new nlobjSearch object. The nlapiSearchRecord API returns an array of nlobjSearchResult objects, and one can easily obtain the length of this array to get the total number of search result items. In contrast, the runSearch() function returns a single object, nlobjSearchResultSet, that encapsulates all of the search results. Currently, there is no direct way to determine the total number of items included in this object.

A way to obtain the total number of search result items in an nlobjSearchResultSet object is to create a helper function similar to the one below:

function searchCustomers()
{
        var srchObj = nlapiLoadSearch('customer', 'customsearch75');
        var results = srchObj.runSearch();
        var length = getResultsLength(results);
}

function getResultsLength()
{
        var length = 0;
        var count = 0, pageSize = 100;
        var currentIndex = 0;
        do{
                count = results.getResults(currentIndex, currentIndex + pageSize).length;
                currentIndex += pageSize;
                length += count;
        }
        while(count == pageSize);
        return length;
}

No comments:

Post a Comment