我希望能够根据销售订单计算销售订单上项目的总权重,并将值存储在自定义字段中。我在此之前创建了一个提交脚本。自定义字段设置为十进制数字类型,并且选中了存储值框,但在销售订单页面上的字段下没有显示任何内容。
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);
}
}
答案 0 :(得分:2)
您需要解析行值:
var quantitycommitted = parseInt(nlapiGetLineItemValue('item','quantitycommitted', i),10) || 0;
var weight = parseFloat(nlapiGetLineItemValue('item','custcol_individual_weight', i)) ||0;
同样在某些情况下,您的位置ID不会是字符串,因此也可能是问题。依赖于==
代替===
。