Is it possible to access the current iteration ind...
# suitescript
c
Is it possible to access the current iteration index inside the .each method on a search resultSet, or in any other way determine whether or not I'm currently at the last iteration?
w
Initialize a variable outside of the each() and then increment it inside of the each?
c
I want to do something in every iteration apart from the last one. I'm not sure how your suggestion would tell me when I'm on the last one?
If I had the length of my search result then sure, I could create my own iteration tracker and compare that to my total number of results, but I'm not sure how I'll find the total number either
s
you could add a formula numeric column, put
ROWNUM
in there and sort the search by that
w
runPaged().count (but that's will probably execute the search twice)
c
@Sandii That would give me numbers to compare to the total number of rows, yes, but I would still not know the total number right?
s
The
.each
isnt really designed to care about which line is which, maybe the better question is why dont you want to execute on the last line and perhaps theres a different way to do it
c
@Watz Yes, I'm looking into that one as well.
If I use runPaged instead of run, then I would skip .each and do .fetch instead. That would only execute the search once right?
I mean, the .count if only looking at the information in the PagedData object to find the total number, it's not actually executing the search if I'm reading the documentation correctly...
s
The convenience of the
.each
is really nice, you can definitely just build your own iterator on the pagedResults (which you have no choice but to do if your results can reach over the
.each
limit anyway)
c
Yes, I think that will have to be my solution. It would still be nice to know if it is possible to access the iteration index inside the .each loop 🙂
Thank you both for your help!
s
the
result
is all you have in the
.each
, there is no index on it and there is no count of total lines
If you care about those things, you might not want to use
.each
, although it seems weird to do somethign to every single line in a searchResult except the last one
c
most things in programming seem weird to me 😉
I'm building a search filter expression array based on my first search results and want to push a 'OR' in between each filter expression. Naturally I don't want the at the very end of the array. I'm confident there's a better way to do this, there usually is 🙂, but this is the way I've thought of so far
s
yeah, just pop the OR off after the
.each
Copy code
var newFilterArr;
//.each
newFilterArr.pop();
Be wary the longer that OR list grows, the slower you should expect that next search to be.
c
So I was correct all along, there is a better way to do it 😄 Thank you again!
r
I would just build the filter expression outside the each after getting the results. While it does cost some processing, it's negligible compared to API calls. Or Sandii's suggestion
m
Strongly advise using a formula instead of using multiple ORs. We had a search that was taking 4 minutes with a filter expression and changed to a couple of seconds with
CASE WHEN {field} IN (....)
c
@reptar That was my initial approach but I was hoping to reduce the number of loops this way. Thank's for replying!
@michoel I can see how that makes sense. In this case I know the filter expression will never be very long, but it would still make sense to improve design. After all, you always 'know' things until it turns out you didn't know...