Netsuite事务列字段重新计算

时间:2014-10-08 15:25:49

标签: netsuite

我创建了一个名为Margin的新事务列字段,并将其添加到销售订单中。我写了一个脚本来根据项目字段计算保证金,但是如果你回去编辑这些值,脚本就不会重新计算。如何在用户编辑值时重新计算?

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');


   }
}

3 个答案:

答案 0 :(得分:0)

您确定item字段的更改是否会触发事后采购事件?你看到你的日志消息吗?您可以尝试移动到Field Changed事件,只是为了查看是否有效。您也可以尝试Recalc活动。

顺便说一下,出于性能原因,我建议在客户端进行控制台日志记录而不是nlapi日志记录。记录回NetSuite会强制为每个日志语句创建HTTP请求和记录。

答案 1 :(得分:0)

因此,在postSourcing事件中,您有两个参数:type是更改的子列表的内部id,name是更改的字段的内部id。

所以在上面粘贴的代码中:

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

您只是触发此代码才能在'项目' item子列表中的字段更改。你有两个选择: 将单价和单位成本字段添加到您的“如果'声明

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

或让它在整个项目列表上运行

if(type == 'item')

答案 2 :(得分: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);  
    }

}