Good morning, I have a challenge with a for loop, ...
# suitescript
n
Good morning, I have a challenge with a for loop, that populates a list from a save search (Working with SS2.0). As the list is created, I want to watch for a variable on the next line, once the variable no longer matches. I want to insert a blank line with an if statement separating the lines by the matching group of lines (item lines by quote). I thought I could achieve this with another for loop with a
*j = i + 1*;
but its looking at the very last line of the list, and not the next line.
s
Lists are pretty weird with their lengths not being dynamic. Is there a reason you need to use LIST instead of SUBLIST? Sublists are much easier to deal with inserting and such.
n
Hi Sandii, The list is a saved search, of sublist items from multiple records. I'm working in a Suitelet.
The Suitlet produces a pdf with this data.
s
not sure I follow that whole paragraph but sounds like an opportunity to use
.groupBy()
then
.forEach()
result group you'd add the batch of results and a blank line.
nested loops are more a 1990's thing. I avoid them because they tend to make things harder to reason about.
n
My apologies, im probably using the wrong terminology.
message has been deleted
I have a scripted search that pulls in the data in. I have it sorted, grouped and such. I then need to manipulate the save search data, and separate it by quote# group.
in the screenshot, Quote # 71389 displays a quote #, which its not the (next quote #) below it for each line (lol not what want). I want to insert a line break between each quote group of listed items.
p
just have a variable that tracks the current quote#, and compare it to the latest one. if it’s different, insert the linebreak first and update the current quote#. need to initialise etc before the for loop, but easy stuff. unless i’m missing something
n
That's what I'm aiming for 🙂. For some reason my next for loop i have, is not looking at the line below it's looking at the very last line of the list. In the screen shot where quote # 71389 is displayed it should still be 71389, until it hits the last line of 71389, and would be 73575, and insert a line break to separate the list
Copy code
...
    var bomResults = itemSearchObj.run().getRange(0, 1000);
    for (var i = 0; i < bomResults.length; i++) {
      var tranId = bomResults[i].getValue({
        name: 'tranid'
      });
      for (var j = i + 1; j < bomResults.length; j++) {
      var tranId2 = bomResults[j].getValue({
        name: 'tranid'
      });
      var category = bomResults[i].getValue({
        name: 'formulatext3'
      });
...
I'm doing something wrong here
b
Maybe im misunderstanding, but why the nested loop
Why not use bomResults[i+1]
p
`
Copy code
var bomResults = itemSearchObj.run().getRange(0, 1000);

var currentsection=bomResults[0].getValue({
                                               name: 'tranid'
                                       });

for (var i = 0; i < bomResults.length; i++) {
  var tranId = bomResults[i].getValue({
                                            name: 'tranid'
                                    });
    if (currentsection!=tranId) {
        currentsection=tranId;
        // do your line break here 
    }
// rest of your loop code 

}
`
or what @battk said. you were over thinking it
n
Lol, I over think all the time, and make it more complicated then it needs to be. I'm still pretty new to dev.
Thank you for the suggestions, its much appreciated. This should be simple. I'll try this out.
NS doesn't seem to like the bomResults[i+1]. I get org.mozilla.javascript.EcmaError: TypeError: Cannot call method "getValue" of undefined (/SuiteScripts/SS2.0/Suitelet.js#630)
It must be because its getting to the last line and going past it for the undefined value.
Yup that was exactly it. I did this to resolve it.
Copy code
...

for (i = 0, j = i + 1; i < bomResults.length, j < bomResults.length; i++, j++) {
      var tranId = bomResults[i].getValue({
        name: 'tranid'
      });
      var tranId2 = bomResults[j].getValue({
        name: 'tranid'
      });
...
Thanks everyone for the help 🙂