I think this might be kind of a stupid question, i...
# suitescript
j
I think this might be kind of a stupid question, is there a way for me to get the total number of results from a search before/after I run it? I have a suitelet that’s loading a search and then running it, using
.each()
to loop through the results just fine, but is there any way to result the number of results before/without looping through them? I’m migrating this script from SS 1.0 and previously, I was able to get the results all at once, parse the JSON and determine the length from there, but that doesn’t seem to be an option. Am I missing something?
m
When you run in paged mode you can query the row count. https://system.app.netsuite.com/app/help/helpcenter.nl?fid=section_4486607957.html
c
Copy code
const MY_SEARCH = search.create({...}); // search.load whatever you need

const MY_SEARCH_RESULTS = MY_SEARCH.runPaged({
        pageSize: 1000
});

MY_SEARCH_RESULTS.pageRanges.forEach(function(pageRange) {
    MY_SEARCH_RESULTS.fetch({index: pageRange.index}).data.forEach(function(searchResult) {

        // do whatever with search result
    });
});
I just use this template 99.99% of the time.
m
If your swapping script versions you might want to consider swapping to the
N/query
module as well. It provides a
.runAsMapped()
method that turns into an object. Along with ability to do multiple joins.
@creece you just made me realize I should really create a bunch of code snippets like that since I am always going to previous repos to remind myself of the syntax.
c
Yeah thats what I do. Different files/searches/lookups etc.. You can add them as templates in webstorm and just insert them wherever