创建销售订单时超出 RangeError 最大调用堆栈大小

RangeError Maximum call stack size exceeded on Creating Sales Order

我们正在开发一个客户端脚本,它将自动更新费率或单价(*这是一个自定义列),但它会抛出一个错误,我认为这是因为如果我们更改费率,它会自动更新单位价格和单价更新后,它也会更新费率,依此类推,直到达到最大调用堆栈,我们正在寻找任何解决方法以防止这种情况发生,请参阅下面的代码。谢谢

function fieldChanged(type, name, linenum)
{
    var context = nlapiGetContext();
    var user = context.getUser();
    var execution = context.getExecutionContext();

try {
    switch (name) {
        case "custcol_cqwst_po_uprice":
            if (execution == "userinterface") {
                var qty = nlapiGetCurrentLineItemValue('item', 'quantity');
                var taxCode = nlapiGetCurrentLineItemValue('item', 'taxcode');

                if (!isNullOrEmpty(taxCode) && !isNullOrEmpty(qty)) {

                    var taxRate = nlapiGetCurrentLineItemValue('item', 'taxrate1');
                    var unitprice = nlapiGetCurrentLineItemValue('item', 'custcol_cqwst_po_uprice');
                    var vatRate = 1 + ((taxRate.replace('%', '')) / 100);
                    var unitRate = unitprice / vatRate;
                    nlapiSetCurrentLineItemValue('item', 'rate', unitRate);
                }
            }
            break;
        //case "taxcode":
            //break;

        case "rate":
            if (execution == "userinterface") {
                var qty = nlapiGetCurrentLineItemValue('item', 'quantity');
                var taxCode = nlapiGetCurrentLineItemValue('item', 'taxcode');

                if (!isNullOrEmpty(taxCode) && !isNullOrEmpty(qty)) {

                    var taxRate = nlapiGetCurrentLineItemValue('item', 'taxrate1');
                    var rate = nlapiGetCurrentLineItemValue('item', 'rate');
                    var vatRate = 1 + ((taxRate.replace('%', '')) / 100);
                    var unitPrice = rate * vatRate;
                    nlapiSetCurrentLineItemValue('item', 'custcol_cqwst_po_uprice', unitPrice);

                    nlapiLogExecution('debug', "Rate Value", "Rate: " + rate + " Vat Rate: " + vatRate + " Unit Price: " + unitPrice + " Execution: " + execution);
                }
            }
            break;
    }
}
catch (ex) {
    alert("A scripting problem occurred during the onFieldChange event please inform an administrator quoting the following error: " + ((ex instanceof nlobjError) ? ex.getCode() + '\n' + ex.getDetails() : ex.toString()));
}
}

nlapiSetCurrentLineItemValues() 有第四个参数 firefieldchanged,可以将其设置为 false 以防止出现此问题

Using the Fire Field Changed Parameter

When creating scripts that provide the ability to watch a field for a change, and then write back to the field that changed, a risk of creating an infinite loop exists as follows:

  1. The Client script watches for fieldA to change.
  2. fieldA changes.
  3. The script writes to fieldA, causing the Field Changed event to fire, returning the code to step 2, and this loop repeats indefinitely.

To prevent this looping behavior, you can set the optional firefieldchanged parameter in your client scripts.

The firefieldchanged parameter is available for all write functions. If set to true, the parameter causes any field changed events to fire as normal. This is the default setting. If set to false, field changed events are NOT fired.