Suitescript stop order from being approved before approval

1.6k views Asked by At

I am working on a script for net suite. The point of the script is to check a sales order to make sure the credit limit is higher than the order total before actually approving the order. The script runs when the user clicks the "approve" button on the sales order. I successfully check to see if its a sales order and if the action type is approve. What I'm not understanding is how to stop the approval if the order is indeed over the credit limit. I tried setting the order status back to pending approval and submit the order but it doesn't work like that. The status is not getting set. There is also no cancel type of command.

     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;
   }
2

There are 2 answers

2
Binoy Samuel On BEST ANSWER

You could throw an exception and thereby abort the submit action. In your beforeSubmit() function, you can have something like:

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

Your code works but you need to change nlapiSetFieldText('orderstatus', 'A'); to nlapiSetFieldValue('orderstatus', 'A');

I used it on a script that compares the total amount of an SO and changes the approval status if it has been increased.