简体   繁体   中英

How do I get my for loop to include the last line in a user event script in NetSuite?

I have a user event script in NetSuite that loops through all sales orders line items and automatically adds deposits based on a few conditions. It works for all lines except the last line.

Almost Working code:

for(var i=0;i<=lineCount;i++){

  var sub_field2 = load_so.getSublistValue({
                                           sublistId: 'item',
                                           fieldId: 'item',
                                           line: i
                                       });
                                       log.debug({
                                         title:"Item ID",
                                         details: sub_field2
                                       });
   var sub_field1 = load_so.getSublistValue({
                                            sublistId: 'item',
                                            fieldId: 'custcol_vcc_deposit_item',
                                            line: i
                                        });
                                        log.debug({
                                          title:"Deposit Item?",
                                               details: sub_field1
                                        });
      var isclosed = load_so.getSublistValue({
                                             sublistId: 'item',
                                             fieldId: 'isclosed',
                                             line: i
                                         });



                                           if(sub_field1 == true && isclosed !== true){
                                             var linkeddepitem = load_so.getSublistValue({
                                                      sublistId: 'item',
                                                      fieldId: 'custcol_vcc_ldi',
                                                      line: i
                                            });
                                            log.debug({
                                              title:"Linked Item ID",
                                              details: linkeddepitem
                                            });

                                            var depqty = load_so.getSublistValue({
                                                     sublistId: 'item',
                                                     fieldId: 'quantity',
                                                     line: i
                                           });
                                           log.debug({
                                            title:"Qty",
                                                 details: depqty
                                           });


                                            load_so.insertLine({
                                              sublistId: 'item',
                                              line: i+1
                                            });
                                            load_so.setSublistValue({
                                              sublistId: 'item',
                                              fieldId:'item',
                                              line: i+1,
                                              value: linkeddepitem
                                            });
                                            load_so.setSublistValue({
                                              sublistId: 'item',
                                              fieldId:'quantity',
                                              line: i+1,
                                              value: depqty
                                            });
                                            var lineCountduringloop = load_so.getLineCount({ sublistId: 'item' });
                                            log.debug({
                                              title:"Line Count Before Return",
                                                   details: lineCountduringloop
                                            });


          };


};

How do I make sure the loop is actually going through the last line? The logs indicate the script stops one line short of where it should be, ie the last line inserted is checked for conditions, which it correctly fails, and then the script exits the loop; it does not even run on the final line.

Thanks for any input!

If lineCount is the total number of lines, the index for the last line (in SS 2.0) will be lineCount - 1 . So your for statement's first line should be:

for(var i=0;i<lineCount;i++){ 

NOT

for(var i=0;i<=lineCount;i++){

(Note the deleted "=")

It appears what's happening is that the script is attempting an operation on a line that doesn't exist; hence the error. This is commonly known as an off by one error.

What Krypton said.

In addition, you need to increment your loop variable (i) when you add a line to the sublist, and also increment the line count. Hopefully that makes sense. This way the total number of line items is represented, and your loop will skip the newly added line.

for(var i = 0; i < lineCount; i++)
{
    ....
    if(sub_field1 === true && isclosed !== true)
    {
        ....
        load_so.insertLine({ sublistId: 'item',
                             line: i+1
                          });
        i++;
        linecount++;
        ....
    }
}

This really would be better as a while loop, since using a for makes it look like its iterating over a fixed number of items.

And with respect to the 'off by one issue', I get a lot of mileage by remembering what is a count (1 based), and what is an offset (0 based). The length of an array is a count, and thus is one based. But the last item's index (as an offset) is 0 based.

 list = ['a', b', 'c', 'd'];
 list.length === 4
 list[list.length -1] === 'd'

 // get second pair
 index = 1; // an index (first pair has the index of 0).
 lengthOfGroup = 2;  // This is a count.
 indexWithinGroup = 0;  // another index
 list[index * lengthOfGroup + indexWithinGroup] === 'c'

 indexWithinGroup = 1;
 list[index * lengthOfGroup + indexWithinGroup] === 'd'

The above gets a lot harder when things are one based. Especially when you are using a javascript API (I'm looking at you, suitescript 1.0..).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM