如果项目有重量,则使用重新计算功能更改字段

Using recalc function to change a field if item has weight

我目前有一个重新计算功能,可以在订单项发生更改时重置运费字段,但我真的只需要在项目有重量的情况下这样做。如何检索更改后的订单项的权重?

这是我目前拥有的:

function recalc(){  
    nlapiSetFieldValue('shippingcost', '0.00');
}

小 segue,recalc 没有提供获取子列表当前行的方法,只要更改了一行,您就需要遍历整个子列表。

试试 validateLine,类似于:

function validateLine(listType){

    //To get the item weight, you could create a 
    //custom transaction column field that sourced the item weight.

    if(nlapiGetCurrentLineItemValue(listType,'custcolitemWeight') > 0){
        nlapiSetFieldValue('shippingcost','0.00')
    }

    //or you could source directly from the item record using nlapiLookupField  
    // Depending on your use case either could be  appropriate

    if(nlapiLookupField('item',nlapiGetCurrentLineItemValue(listType,'item'),'weight')){
        nlapiSetFieldValue('shippingcost','0.00')
    }
    //you *need* to return true with the validate* event functions. 
    return true;
}

此(未经测试的)示例仅处理行添加。如果允许用户删除项目,您将需要实施类似的 validateDelete 来还原您的更改。

recalc 仅在 行项目的更改影响交易的 Total 时触发,因此,可能不是您想要完成的可靠事件。

我建议不要使用 validateLine,因为应该使用该事件来确定字段的新值是否有效。

我建议您使用 fieldChanged 来响应已更改的字段值。类似于:

function fieldChanged(type, name, linenum) {
    if (type == 'item') {
        if (name == 'item') {
            handleItemChange(linenum);
        }
    }
}

function handleItemChange(linenum) {
    var itemWeight = parseFloat(nlapiGetFieldValue('item', 'weight', linenum)) || 0;
    if (itemWeight > 0) {
        nlapiSetFieldValue('shippingcost', 0);
    }
}

您可能还想考虑 postSourcing 事件而不是 fieldChanged,具体取决于哪些字段应实际触发此逻辑。