SuiteScript 根据发货地点求和重量

SuiteScript to sum the weight based on ship-out location

我希望能够根据发货地点计算销售订单上商品的总重量,并将这些值存储在自定义字段中。我在提交脚本之前创建了一个。自定义字段设置为小数类型,并勾选了储值框,但在销售订单页面的字段下没有任何显示。

function calculateWeight(type){

  var lines = nlapiGetLineItemCount('item');

  var totalWeight2 = 0 ;
  var totalWeight1 = 0 ;

  if (lines >0){
      for(var i = 1; i<= lines ; i++){
          var location = nlapiGetLineItemValue('item','location', i);
          var quantitycommitted = nlapiGetLineItemValue('item','quantitycommitted', i);
          var weight = nlapiGetLineItemValue('item','custcol_individual_weight', i);
          //var com_wgt = nlapiGetLineItemValue('item','custcol1',i);

          if (location === '2'){
              var total2 = weight * quantitycommitted;

              totalWeight2 += total2 ;
          }

          if (location === '1'){
              var total1 = weight * quantitycommitted;

              totalWeight1 += total1 ;
          }

      }

      nlapiSetFieldValue('custbody5', totalWeight1);
      nlapiSetFieldValue('custbody4', totalWeight2);

  }

}

我仍在学习 SuiteScript,我不确定哪里出了问题...有人可以帮忙吗?


更新代码,仅适用于部分订单...

function calculateWeight(type){

  var lines = nlapiGetLineItemCount('item');
  //nlapiLogExecution('DEBUG', 'Number of lines', lines);

  var totalWeight2 = 0 ;
  var totalWeight1 = 0 ;

  if (lines >0){
      for(var i = 1; i<= lines ; i++){
      var location = nlapiGetLineItemValue('item','location', i);
    //nlapiLogExecution('DEBUG', 'Locations', location);
      var quantitycommitted = parseInt(nlapiGetLineItemValue('item','quantitycommitted', i),10) || 0;
    //nlapiLogExecution('DEBUG', 'QtyCom', quantitycommitted);
      var weight = parseFloat(nlapiGetLineItemValue('item','custcol_individual_weight', i)) ||0;
     //nlapiLogExecution('DEBUG', 'Wgt', weight);

    //var com_wgt = nlapiGetLineItemValue('item','custcol1',i);

      if (location == '2'){
          var total2 = weight * quantitycommitted;

          totalWeight2 += total2 ;
        nlapiLogExecution('DEBUG', 'Total2', totalWeight2);

      }

      if (location == '1'){
          var total1 = weight * quantitycommitted;

          totalWeight1 += total1 ;
        nlapiLogExecution('DEBUG', 'Total1', totalWeight1);
      }

  }

  nlapiSetFieldValue('custbody_ms_weight_ppt_page', totalWeight1);
  nlapiSetFieldValue('custbody_wi_weight_ppt_page', totalWeight2);

 }

}

您需要解析行值:

var quantitycommitted = parseInt(nlapiGetLineItemValue('item','quantitycommitted', i),10) || 0;
var weight = parseFloat(nlapiGetLineItemValue('item','custcol_individual_weight', i)) ||0;

在某些情况下,您的位置 ID 也不是字符串,因此这也可能是问题所在。依靠 == 而不是 === 是可行的。