When I create a saved search using the 'each' meth...
# suitescript
w
When I create a saved search using the 'each' method, I notice it consumes 10 units of governance using the methods 'getCurrentScript().getRemainingUsage()' from the runtime module. However, when I use a paginated search, I don't see any consumption of governance units. Does anyone know why?
e
According to the Help docs, the
runPaged()
and
fetch()
methods each use 5 units.
currentPage.data
is just a standard JS Array; the JS
forEach
method is not going to consume any governance.
a
Adding to @erictgrubaugh comment, consider using: •
runPaged({pageSize: 1000})
If you don't use
pageSize
with the max value, your search could run out of governance with a very large search because it will use the default pageSize(~100).
w
But then, when using runtime.getCurrentScript().getRemainingUsage(), it should show the governance units used, but it starts with 1000, and the last log shows me that it ends with 1000.
t
@William Rodriguez I can confirm you're not crazy
Copy code
require(['N/runtime', 'N/search'], function (runtime, search) {
    try {


        var searchObj = search.create({
            type: "transaction",
            filters:
                [
                    ["mainline", "is", "T"]
                ],
            columns:
                [
                    search.createColumn({
                        name: "ordertype",
                        sort: search.Sort.ASC
                    }),
                    "mainline",
                    "trandate",
                    "asofdate",
                    "postingperiod",
                    "taxperiod",
                    "type",
                    "tranid",
                    "entity",
                    "account",
                    "memo",
                    "amount",
                ]
        });
        var searchResultCount = searchObj.runPaged().count;
        log.debug("searchObj result count", searchResultCount);

        var list = [];
        var page;
        var pagedData;

        pagedData = searchObj.runPaged({ pageSize: 1000 });
        pagedData.pageRanges.forEach(function (pageRange) {
            page = pagedData.fetch({ index: pageRange.index });
            page.data.forEach(function (row) {
                //log remaining usage


                return true;
            });
            log.debug('remaining usage after fetch' + pageRange.index, runtime.getCurrentScript().getRemainingUsage());
            return true;
        });

        log.debug('remaining usage at end', runtime.getCurrentScript().getRemainingUsage());

    } catch (e) {
        var scriptId = runtime.getCurrentScript().id;
        log.error('ERROR:' + scriptId + ':fn:' + runtime.executionContext, JSON.stringify({ type: e.type, name: e.name, message: e.message, stack: e.stack, cause: JSON.stringify(e.cause), id: e.id }));
    }
});