2016-10-03 55 views
1

我写在NetSuite的JavaScript的SuiteScript一个脚本,它的项目的子表从报价或销售订单,并在编辑或创建,添加或关于该项目的定价子列表更新这些项目在客户记录上。suitescript添加项子表到itempricing子列表 - 提高性能

所以2列出了我比较(rec.itempricing和newSoQuote.item),然后更新rec.itempricing

我的问题:我有具有三重for循环(显然)的性能问题;我如何结合这些for循环中的一些行来减少执行时间?下面

代码:

function userEventBeforeSubmit(type){ 
var currentUserRole = nlapiGetRole(); 
if (type != 'create' && type != 'edit') 
    return; 

if (currentUserRole != 1037 && currentUserRole != 1064) 
    return; 

var newSoQuote = nlapiGetNewRecord(); 
var custInternalId = newSoQuote.getFieldValue('entity'); 
var soItemsCount = newSoQuote.getLineItemCount('item'); 
var rec = nlapiLoadRecord('customer',custInternalId); //load customer record 
var quotedPrice, duplicate, itemType; 
var itemPricingCount; 

for (var i = 1; i <= soItemsCount; i++) { 
    duplicate = false; 
    itemType = newSoQuote.getLineItemValue('item', 'itemtype', i); 
    if(itemType == 'InvtPart') { 
     itemPricingCount = rec.getLineItemCount('itempricing'); 
     //get the rate (unit price) 
     quotedPrice = newSoQuote.getLineItemValue('item', 'rate', i); 
     //check for duplicate 
     for (var j = 1; j <= itemPricingCount; j++) { 
      //if we find a match on itempricing list 
      if (newSoQuote.getLineItemValue('item','item',i) == rec.getLineItemValue('itempricing', 'item', j)){ 
       rec.selectLineItem('itempricing', j); 
       rec.setCurrentLineItemValue('itempricing', 'price', quotedPrice); //update price 
       rec.commitLineItem('itempricing'); //commit the line item 
       j = itemPricingCount + 1; 
       duplicate = true; 
      } 
     } 
     for (var k = 1; k < i; k++) { 
      if (newSoQuote.getLineItemValue('item', 'item', i) == newSoQuote.getLineItemValue('item', 'item', k)) 
       duplicate = true; 
     } 
     if (!duplicate) { //if not on the itempricing sublist adding new item 
      rec.selectNewLineItem('itempricing'); //select a new line on the item price sublist 
      rec.setCurrentLineItemValue('itempricing', 'item', newSoQuote.getLineItemValue('item', 'item', i)); //select an item 
      rec.setCurrentLineItemValue('itempricing', 'level', -1); //select 'custom' level 
      rec.setCurrentLineItemValue('itempricing', 'currency', 1); //select currency 
      rec.setCurrentLineItemValue('itempricing', 'price', quotedPrice); //input price 
      rec.commitLineItem('itempricing'); //commit the line item 
     } 
    } 
} 
nlapiSubmitRecord(rec); //submit the record 

}

我已经感动了所有的变量声明出来的for循环,但似乎一切必要...

感谢所有,任何帮助表示赞赏。

回答

0

您可以在rec对象上使用findLineItemValue方法。它的工作原理如下:

var lookForItem = newSoQuote.getLineItemValue('item','item',i); 
var foundIndex = rec.findLineItemValue('itempricing','item',lookForItem); 
/* if foundIndex is -1, the item was not found, exlist it would be the row index in sublist */ 
if(foundIndex==-1) { 
    rec.selectNewLineItem('itempricing'); 
    //... and set the rest of your sublist values. 
    rec.commitLineItem('itempricing'); //commit the line item 
} 

让我知道你是否需要补充说明。

+0

这是完美的。谢谢! – Graybies

0

我认为你的主要问题是你在for循环中使用selectNewLineItem,commitLineItem和'selectLineItem`。这些操作依赖于相对较慢的DOM操作和Ajax传输。通常这是不可避免的。

只有更快的方式来完成你需要的是使用后端事件脚本,它可以操纵子列表没有所有的DOM/GUI操控什么。这是一种有点不同的方法,因为脚本只在GUI呈现之前运行,或者保存在两者之间。

P.S.对于小的潜在速度增益,请尝试禁用setCurrentLineItemValue上的事件触发器。要禁用,您需要设置方法参数之一false