2014-10-08 49 views
0

我創建了一個名爲Margin的新交易列字段,並將其添加到銷售訂單。我編寫了一個腳本根據項目字段計算頁邊距,但如果您返回編輯這些值,則腳本不會重新計算。如何在用戶編輯值時重新計算它?Netsuite交易列字段重新計算

function postSourcing(type, name){ 
    if(name == 'item'){ 
    var unitPrice = nlapiGetCurrentLineItemValue('item','rate'); 
    nlapiLogExecution('DEBUG', 'Price', + unitPrice); 
    var itemid = nlapiGetCurrentLineItemValue('item', 'item'); 
    var avgCost = nlapiGetCurrentLineItemValue('item','lastpurchaseprice'); 
    nlapiLogExecution('DEBUG', 'Cost', + avgCost); 

    var marginCalc = Math.round((((unitPrice - avgCost)/unitPrice)*100) * 10)/10; 
    nlapiLogExecution('DEBUG', 'Margin', + marginCalc); 
    nlapiSetCurrentLineItemValue('item','custcol9', marginCalc); 
    avgCost = Math.round(avgCost * 100)/100; 
    nlapiSetCurrentLineItemValue('item','lastpurchaseprice', avgCost); 


    var dollarPro = unitPrice - avgCost; 
    nlapiSetCurrentLineItemValue('item','custcoldollarprofit', dollarPro); 

    var itemQty = nlapiGetCurrentLineItemValue('item','quantity'); 
    var extDollar = dollarPro * itemQty; 
    nlapiSetCurrentLineItemValue('item','custcolextdollarprofit',extDollar); 
    nlapiCommitLineItem('item'); 


    } 
} 

回答

0

因此,在postSourcing事件中,您有兩個參數:type是更改的子列表的內部標識,name是更改的字段的內部標識。

所以在你的代碼粘貼以上:

function postSourcing(type, name){ 
if(name == 'item'){ 

你只觸發此代碼運行時,在該項目的子表的變化「項目」字段。你有兩個選擇: 加上單位價格和單位成本字段的「如果」語句

if(name == 'item' || name == 'lastpurchaseprice' || name == 'rate') 

還是有它整個項目列表

if(type == 'item') 
+0

非常感謝你解釋這個!我對Netsuite的腳本還不熟悉。 :)似乎現在工作正常。 – Laura 2014-10-13 13:43:55

0

您確定對item字段的更改正在觸發Post-Sourcing事件嗎?你看到你的日誌消息嗎?您可以嘗試轉移到Field Changed事件,而不是僅僅看看是否有效。您也可以嘗試Recalc事件。另外,爲了提高性能,我建議在客戶端使用控制檯日誌記錄,而不是nlapi日誌記錄。記錄回NetSuite將強制每個日誌語句的HTTP請求和記錄創建。

+0

它似乎只能用作發佈源事件。如果你不編輯任何東西,它確實有效,但這是問題。如果您嘗試更改價格,價值保持不變。 – Laura 2014-10-08 18:05:26

+0

如果您希望在編輯價格時執行腳本,則需要爲字段更改事件添加一個函數。 – 2014-10-08 21:20:22

+0

@RustyShackles是正確的。現在,您只有一個後期採購活動,您只能在'item'字段發生變化時才運行。如果還有其他字段需要響應,則需要將相應的字段ID添加到Post-Sourcing或Field Changed事件處理程序中,如'function fieldChanged(type,name){if(name == 'price'|| name =='rate'|| name =='quantity){//在這裏重新計算邊距}'我建議將重新計算邏輯提取到它自己的函數中,並簡單地在適當的事件上調用該函數。 – erictgrubaugh 2014-10-08 22:09:48

0

這裏上運行是我結束了。謝謝大家的提示!這現在工作100%。我希望這能夠幫助任何試圖做同樣事情或類似事情的人。

/* 
* Author: Laura Micek 
* Date: 10/20/14 
* Purpose: Populates the margin, $ profit and ext. $ profit custom fields on various transaction forms. 
* Uses a postsource event for the initial values and the field change event for the recalculation of values. 
*/ 

function postSourcing(type, name) { 

if(type == 'item') { 

    var unitPrice = nlapiGetCurrentLineItemValue('item','rate'); 
    var avgCost = nlapiGetCurrentLineItemValue('item','lastpurchaseprice'); 

    /* calculates margin by subtracting average cost from unit price and then dividing by average cost. 
    * It is then multiplied by 100 to get the percent. Used Math.round to round to one decimal place. */ 
    var marginCalc = Math.round((((unitPrice - avgCost)/unitPrice)*100) * 10)/10; 
    nlapiSetCurrentLineItemValue('item','custcol9', marginCalc); 
    avgCost = Math.round(avgCost * 100)/100; 
    nlapiSetCurrentLineItemValue('item','lastpurchaseprice', avgCost); 

    var dollarPro = unitPrice - avgCost; 
    nlapiSetCurrentLineItemValue('item','custcoldollarprofit', dollarPro); 

    var itemQty = nlapiGetCurrentLineItemValue('item','quantity'); 
    var extDollar = dollarPro * itemQty; 
    nlapiSetCurrentLineItemValue('item','custcolextdollarprofit',extDollar); 

    } 

} 

function reCalc(type, name) { 

/*intialized the unit price here so that I could check if it was above zero. 
If you don't check, it will try to recalculate based on zero and you will 
get a invalid percent error*/ 
var unitPrice = nlapiGetCurrentLineItemValue('item','rate'); 

//check if quantity was updated 
if(name == 'quantity') { 
    var unitPrice = nlapiGetCurrentLineItemValue('item','rate'); 
    var avgCost = nlapiGetCurrentLineItemValue('item','lastpurchaseprice'); 

    var dollarPro = unitPrice - avgCost; 
    nlapiSetCurrentLineItemValue('item','custcoldollarprofit', dollarPro); 

    var itemQty = nlapiGetCurrentLineItemValue('item','quantity'); 
    var extDollar = dollarPro * itemQty; 
    nlapiSetCurrentLineItemValue('item','custcolextdollarprofit',extDollar); 
} 

//checks if the unit price was change and if the price is greater than zero 
if(name == 'rate' && unitPrice > 0) { 
    unitPrice = nlapiGetCurrentLineItemValue('item','rate'); 
    var avgCost = nlapiGetCurrentLineItemValue('item','lastpurchaseprice'); 

    /* calculates margin by subtracting average cost from unit price and then dividing by average cost. 
    * It is then multiplied by 100 to get the percent. Used Math.round to round to one decimal place. */ 
    var marginCalc = Math.round((((unitPrice - avgCost)/unitPrice)*100) * 10)/10; 
    nlapiSetCurrentLineItemValue('item','custcol9', marginCalc); 
    avgCost = Math.round(avgCost * 100)/100; 
    nlapiSetCurrentLineItemValue('item','lastpurchaseprice', avgCost); 
    } 

} 
+0

確保僅將腳本部署到您將字段添加到的交易表單! – Laura 2014-10-20 17:10:29