0
votes

Ive been trying to create a suitelet that allows for a saved search to be run on a collection of item records in netsuite using suitescript 1.0

Pagination is quite easy everywhere else, but i cant get my head around how to do it in NetSuite.

For instance, we have 3,000 items and I'm trying to limit the results to 100 per page.

I'm struggling to understand how to apply a start row and a max row parameter as a filter so i can run the search to return the number of records from my search

I've seen plenty of scripts that allow you to exceed the limit of 1,000 records, but im trying to throttle the amount shown on screen. but im at a loss to figure out how to do this.

Any tips greatly appreciated

function searchItems(request,response)
{
    var start = request.getParameter('start');
    var max = request.getParameter('max');

    if(!start)
    {
      start = 1;
    }
    if(!max)
    {
      max = 100;
    }

    var filters = [];
    filters.push(new nlobjSearchFilter('category',null,'is',currentDeptID));
    var productList = nlapiSearchRecord('item','customsearch_product_search',filters);
    if(productList)
    {
      response.write('stuff here for the items');
    }
}
1
What version of NetSuite are you using? Is there any reason why you are not using SuiteScript 2.0? They support for paginated queries built-in to the N/search module. - Nadeem Douba

1 Answers

1
votes

You can approach this a couple different ways. Either way, you will definitely need to sort your search results by something meaningful and consistent, like by internal ID. Make sure you've got your results sorted either in your saved search definition or by adding a search column in your script.

You can continue building your search exactly like you are, and then just using the native slice method on the productList Array. You would use your start and end parameters to pass as the arguments to slice appropriately.

Another approach is to use the async API for searches. It will look similar to this:

var search = nlapiLoadSearch("item", "customsearch_product_search");
search.addFilter(new nlobjSearchFilter('category',null,'is',currentDeptID));
var productList = search.runSearch().getResults(start, end);

For more references on this approach, check out the NetSuite Help page titled "Search APIs" and the reference page for nlobjSearch.