is there a way to have a map/reduce exit when it h...
# suitescript
s
is there a way to have a map/reduce exit when it hit 500 iterations ( meaning i have a list of 1000 ) but only want the script to process the first 500 or get the max results of the saved search for max results to work? or does this return the full 1000
Copy code
search.load({
      id: loadid,
    });
e
it would be better if you add filters to get the exact results you want to process, you cannot stop the map stage once the map reduce has started.
s
ok so then in the saved search how can i return only the top 500 sorted
j
From what I know, the input stage can also return an array, so you could use search.load({id: loadid}).run().getRange(0,500)
☝️ 2
c
What i've done is create my own objects to return an array of in getInputData. Since you have your own array, you can then just return a slice of it. Could be a script parameter you set or just a hardcoded value if you need to. I like @Jan Petri’s example too. Just depends if you want to make your own custom objects or just return the results as is.
s
@Jan Petri idea works great . The curve ball is when the data is converted to array vs a search object the names and structure change
c
Well maybe thats another check in the "create your own object" column. I do it so I know exactly what Im getting in my map/reduce stages. You can also trim away stuff you may not need.
j
Yeah @creece has a good point here. The data structure is different depending on whether you return the search object or the result array. Forgot to mention that hehe
s
if you can use SuiteQL that can give you 500 happy JS objects for search results without any real effort via
asMappedResults
or, with NFT and traditional search it would be
return Seq(LazySearch.from(<searchid)).map(nsSearchResult2obj()).take(500).toArray()
s
Does nft slow down exuction scripts
s
No, in some cases, it speeds it up.
s
Really can you share a link how to deploy install
s
The above for example, automatically pages through results, only loading one page at a time into memory. This means it should only load a single page for the example above (500 results max).
s
Coming from a rails background this is great 👍👍
s
Yes, the active-record feel for basic record access is quite nice
👍 1
s
@Sim Greenbaum what’s the use case for only wanting 500 of 1,000? Aside from what has already been suggested, to get only 500 results in a classic search, you could inspect the search result paged, ordered by internalid, for example, and then use the start and end internalid as a filter:
internalidnumber >= MIN and <= MAX
OR, If you want to page by 500, you could return the array of the designated page you want to retrieve in the MAP.. i.e.:
[1]
and the MAP would run a paged search, only returning the paged data which is up to 500 results for that search.
s
the other curve ball is if you return an array the key is the index , as opposed to the intenal id of the search record
Copy code
If the input type is a result set, the key is the internal ID of the result
@suitedev do any of your solution return a result set vs an array ?
s
not in this case.