SuiteScript to fetch more than 4000 results from a saved search

Solution :

var customSearchObj = search.create({
    type: "customrecordcatalog_items_components",
    filters:
        [
            ["custrecorditem", "noneof", "@NONE@"]
        ],
    columns:
        [
            search.createColumn({ name: "internalid", label: "Internal ID" }),
            search.createColumn({ name: "custrecorditem", label: "Item" })
        ]
});
var resultSet = customSearchObj.run();
var catalogItemsArray = [];
var currentRange = resultSet.getRange({
    start: 0,
    end: 1000
});
var i = 0;  // iterator for all search results
var j = 0;  // iterator for current result range 0..999

while (j < currentRange.length) {

    // take the result row
    var result = currentRange[j];
    var catalogItemsObj = {}

    catalogItemsObj.catalogId = result.getValue('internalid')
    catalogItemsObj.itemId = result.getValue('custrecorditem')
    catalogItemsArray.push(catalogItemsObj);

    i++; j++;
    if (j == 1000) {   // check if it reaches 1000
        j = 0;          // reset j an reload the next portion
        currentRange = resultSet.getRange({
            start: i,
            end: i + 1000
        });
    }
}
return catalogItemsArray

Leave a comment

Your email address will not be published. Required fields are marked *