代码之家  ›  专栏  ›  技术社区  ›  Grumpy Bunny

SuiteScript根据发货位置计算重量总和

  •  0
  • Grumpy Bunny  · 技术社区  · 7 年前

    我希望能够根据发货位置计算销售订单上项目的总重量,并将值存储在自定义字段中。在提交此脚本之前,我创建了一个。“自定义”字段设置为“十进制数”类型,并选中“存储值”框,但“销售订单”页面上的字段下未显示任何内容。

    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);
    
     }
    
    }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   bknights    7 年前

    您需要分析行值:

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

    此外,在某些情况下,位置ID不会是字符串,因此这可能也是问题所在。依赖于 == 而不是 === 作品