优化netsuite脚本

时间:2016-12-22 20:25:26

标签: javascript performance netsuite

我有一个包含子列表的记录,该子列表中的每个项目都是具有另一个子列表的记录。我想要的是,对于每个itemA获取每个itemA子列表的所有子列表。喜欢这个

Bill of lading1
  ->FulfillmentA
      ->Item123
      ->Item124
      ->Item125
  ->FulfillmentB
      ->Item224
      ->Item226
      ->Item227

我希望能够为每个提单

创建一个这样的表
Fulfillment | Item | Description | Qty

例如,1份提单有2个履行,每个履行中有3个

               BILL OF LADING #4
Fulfillment | Item | Description | Qty
-------------------------------------------
    A       |  123 |     xxxx    |  2
            |  124 |     xxxx    |  1
            |  125 |     xxxx    |  7
--------------------------------------------
    B       |  224 |     xxxx    |  1
            |  226 |     xxxx    |  1
            |  227 |     xxxx    |  1

我可以这样做,问题出现在某些完整版有60-70次执行时,我的脚本执行使用限制超出错误。

我的代码的基本思想是创建一个这样的对象:

   * { 
   *   70: {
   *     itemNumber : {
   *        description: xxxx,
   *        quantity: XX
   *     },
   *     itemNumber2 : {
   *        description: xxxx,
   *        quantity: XX
   *     },
   *   71: { ...},
   *   ....
   * }

然后用循环创建表。

我认为问题在于这一行:

fulfillmentArray.push(nlapiLoadRecord('itemFulfillment', fullfillmentInternalIDArray[z]));

因为当我在具有100个完整效果的BOL上删除它时,它会起作用

这是我的代码:

var lines = record.getLineItemCount('recmachcustrecord_id_billoflading');
//Grab the fulfillment internal id
for(var i = 1; i <= lines; i++){
    fullfillmentInternalIDArray.push(record.getLineItemValue('recmachcustrecord_id_billoflading', 'custrecord_fulfillment', i) ); 
}

//Get the fulfillment number
for(var x = 1; x <= lines; x++){
    fulfillmentNumberArray.push(record.getLineItemText('recmachcustrecord_id_billoflading', 'custrecord_fulfillment', x).replace(/Fulfillment|#|Item/g,"")); 
}

//Get the fulfillments and push them into an array
//And create an empty object with the fulfillment number
for(var z = 0; z < fullfillmentInternalIDArray.length; z++){
    fulfillmentArray.push(nlapiLoadRecord('itemFulfillment', fullfillmentInternalIDArray[z]));
    billOfLadingFulfillmentsObject[ fulfillmentNumberArray[z] ] = {};
}

然后创建对象的其余部分:

for(var j = 0; j < fulfillmentArray.length; j++){
  itemCount = fulfillmentArray[j].getLineItemCount('item');

  for(var y = 1; y <= itemCount; y++){
      billOfLadingFulfillmentsObject[ fulfillmentNumberArray[j] ][ fulfillmentArray[j].getLineItemValue('item', 'itemname', y) ] = {};
  }  

  for(var item in billOfLadingFulfillmentsObject){
      for(var item2 in billOfLadingFulfillmentsObject[item]){
          for(var y = 1; y <= itemCount; y++){
              if(item2 === fulfillmentArray[j].getLineItemValue('item', 'itemname', y)){
                    billOfLadingFulfillmentsObject[ fulfillmentNumberArray[j] ][item2]['description'] = fulfillmentArray[j].getLineItemValue('item', 'itemdescription', y);
                    billOfLadingFulfillmentsObject[ fulfillmentNumberArray[j] ][item2]['quantity'] = fulfillmentArray[j].getLineItemValue('item', 'quantity', y);
              }
          } 
      }
  }
}

我不知道如何优化,所以当有大量的实现时,它不会导致脚本执行使用限制超出错误。

1 个答案:

答案 0 :(得分:0)

我建议进行搜索以检索大量数据,因为它更便宜。循环收集项目ID的项目并进行搜索以获取数据。