简体   繁体   English

如何让我的 for 循环在 NetSuite 的用户事件脚本中包含最后一行?

[英]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.我在 NetSuite 中有一个用户事件脚本,它循环遍历所有销售订单行项目,并根据一些条件自动添加存款。 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 .如果lineCount是总行数,则最后一行的索引(在 SS 2.0 中)将为lineCount - 1 So your for statement's first line should be:所以你的for语句的第一行应该是:

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.此外,您需要在添加一行到子列表来增加你的循环变量(i),并且增加了行数。 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.这确实作为 while 循环会更好,因为使用 for 使它看起来像是对固定数量的项目进行迭代。

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).关于“一个问题”,通过记住什么是计数(基于 1)和什么是偏移量(基于 0),我获得了很多里程。 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.但是最后一项的索引(作为偏移量)是基于 0 的。

 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..).特别是当您使用 javascript API 时(我正在看着您,suitescript 1.0 ..)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在NetSuite上的“删除”用户事件期间该如何做? - How to do something during the “Delete” user event on NetSuite? Netsuite用户事件脚本:并非所有用户事件类型都会触发 - Netsuite User Event Script: Not all user event types firing 如何让我的页面记住用户的最后操作? - How do I get my page to remember user's last action? 部署用户事件脚本以从Netsuite ERP中部署的所有套件中获取所有字段和值 - Deploy user event script to get all fields and values from all suitelets deployed in Netsuite ERP 我如何从“输入”事件中获取最后的输入 - How do I get the last thing input from an “input” event 如何获取客户端用户的最后一条消息? - How do I Get the Client User's Last Message? Netsuite用户事件脚本正在获取JavaScript全局变量的空值 - Netsuite User Event Script is Getting Empty values for the JavaScript Global Variable 写入 totaltax 之前的 Netsuite 用户事件脚本触发器 - Netsuite User event script trigger before totaltax is written 如何在netsuite上使用用户事件套件脚本更新不支持的交易类型(例如,账单)上的字段 - How can I update fields on unsupported transaction types such as Bill with user event suitescript on netsuite 如何在单击事件的函数中运行用户输入? - How Do I get the user input to be run in the function on click event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM