Suitescript停止订单在批准前被批准

Suitescript stop order from being approved before approval

我正在为网络套件编写脚本。该脚本的要点是在实际批准订单之前检查销售订单以确保信用额度高于订单总额。该脚本在用户单击销售订单上的 "approve" 按钮时运行。我成功地检查它是否是销售订单以及操作类型是否已批准。我不明白的是,如果订单确实超过信用额度,如何停止批准。我尝试将订单状态设置回待批准并提交订单,但它并不像那样工作。状态未设置。也没有取消类型的命令。

     function beforeApproveSalesOrder(type)
    {
        var recordType = nlapiGetRecordType();
        var recordId = nlapiGetRecordId();
        if (recordType == 'salesorder')
        {
           if (type == 'approve')
           {
               var recordId = nlapiGetRecordId();
               nlapiLogExecution("debug", "Check Credit Limit Before Approval", "Found sales order with transaction type of approve.");
               var salesOrderTotalCost = nlapiGetFieldValue('total');
               var customer = nlapiGetFieldValue('entity');
               var customerCreditLimit = queryCustomerCreditLimit(customer);

               if (customerCreditLimit < salesOrderTotalCost)
               {
                  nlapiSetFieldText('orderstatus', 'A');
                  nlapiLogExecution("debug", "Check Credit Limit Before Approval", "order status set");

                  nlapiSubmitRecord(recordId, true);
                }
              }
          }
      }

    function queryCustomerCreditLimit( customer )
    {
        var filters = new Array();
        filters[0] = new nlobjSearchFilter( 'internalid', 'null', 'is', customer);
        var columns = new Array();
        columns[0] = new nlobjSearchColumn( 'creditlimit' );
        var searchresults = nlapiSearchRecord( 'customer', null, filters, columns );
        var creditLimit = searchresults[ 0 ].getValue( 'creditlimit' );
        return creditLimit;
   }

您可以抛出异常,从而中止提交操作。在你的 beforeSubmit() 函数中,你可以有类似的东西:

if (customerCreditLimit < salesOrderTotalCost){
  throw nlapiCreateError('Error','Cannot Approve. Exceeds credit limit',true);
}

您的代码有效,但您需要将 nlapiSetFieldText('orderstatus', 'A'); 更改为 nlapiSetFieldValue('orderstatus', 'A');

我在一个脚本上使用它来比较一个 SO 的总量,如果增加了则更改批准状态。