Quick question: in SS 1.0 you can very easily get ...
# suitescript
b
Quick question: in SS 1.0 you can very easily get the number of search results, as
nlapiSearchRecord
returns a
length
property. What's a straightforward way of achieving the same thing in SS 2.x? For instance, to check whether there are any search results at all. If I want to use
.each
on the resultSet, is the only thing I can do just count the iterations (i.e. no way to get the result set count before that)?
c
there's a ".count" property on the search results after you run the search
Copy code
const ADDITIONAL_SHIPPING_DAYS_SEARCH_RESULTS = ADDITIONAL_SHIPPING_DAYS_SEARCH.runPaged({
    pageSize: 1000
});

log.debug({title: 'ADDITIONAL_SHIPPING_DAYS_SEARCH_RESULS.count', details: ADDITIONAL_SHIPPING_DAYS_SEARCH_RESULTS.count});
👍 1
a
its on the Paged Data, not the search results, the code above it correct, you'd have to runPaged
👍 1
b
@creece Thanks. When I use
search.run()
and then relying on
.each
, there is no count property. So I suppose I must use one of the other methods (runPaged or getRange)?
c
Yeah you'd have to run one of them that gives you the Search.Results[] and then it has a count property on it. I typically always just run a paged search.
👍 1
b
OK, thanks. I suppose it makes sense. I like to use .each when I'm expecting a smallish count, because it saves me writing a few lines of additional code. I'll try runPaged then.
a
I haven't tried this so it might not work... resultSet is in object ... can you do Object.keys(resultSet) to get an array of the keys and then get the .length of that? it might have some additional keys that aren't actual results though... maybe just log the resulting array? maybe the actual results are nested inside one of the keys? it literally iterates over it so it must know how many there are
b
@Anthony OConnor When I inspect the object in the debugger it only shows a columns property. But Object.keys give me
columns,getRange,each,toString,toJSON
.
a
huh... surprising, umm I guess use runPaged then ¯\_(ツ)_/¯
@Benjamin Jung just to clarify you can still use the .each just use the pagedResults to get your count upfront....
Copy code
const searchResultCount = searchObj.runPaged().count;
log.debug("searchObj result count",searchResultCount);

searchObj.run().each(function(result){
    // .run().each has a limit of 4,000 results
    return true;
});
n
I think I'm correct saying doing the above effectively runs the search twice, this could be a performance issue if the result is likely to be a lot of records. I prefer to get all the results as an array and check the length. You can always iterate the array any way, using your preferred method.
✔️ 2
b
nlapiSearchRecord returns an array of at most 1000 search results, and as an array has a nice length parameter the closest equivalent in suitescript 2 is ResultSet.getRange, which also returns an array of at most 1000 search results
1