Using a Third-Party Tax Calculator

Using SuiteScript, you can send order information to a third-party remote server, and then override the tax settings on the sales order based on calculations returned by a third-party.

Setup

The only setup required for this sample script is to replace the sample URL in the script with the actual URL provided by your third-party tax calculator.

Create the Code

          //BEGIN CUSTOM FUNCTIONS FOR THIRD-PARTY TAX

function customOnChange(type, name, linenum)
{ 

        
          // With third-party tax calculations, it is important to capture and override any tax setting.
// The if statement below ensures that tax settings are overridden,but use this technique with
// care, as the setTaxAmount() function also sets the tax. It is critical not to trigger a
// changeField event in this function, so note that the nlapiSetFieldValue is called with the
// appropriate flags.    
if (name == 'promocode' || name == 'taxrate')
   {
      //alert('Onchange called for '+name);
      setTaxAmount();
   }
}
 
function customRecalc(type, action)
{ 

        
          // In SuiteScript for the web store, action can only be 'commit' or 'remove.'    
if (type == 'item')
   {
      setTaxAmount();
   }
} 
// END CUSTOM FUNCTIONS FOR THIRD-PARTY TAX 

        
          // BEGIN THIRD PARTY TAX  

function setTaxAmount()
{ 

        
          // Construct an array of values needed by the third-party tax provider to calculate tax. 

   var postArgs = new Array();
   postArgs['zipCode'] = nlapiGetField('shipzip');
   postArgs['saleTotal'] = nlapiGetField('subtotal'); 

        
          //Replace the URL below with your third-party tax provider.    

var response = nlapiRequestURL("http://www.thirdpartytaxcalculator.com", postArgs, null, null);
   var body = response.getBody(); 

        
          // Parse tax amount from the body. Your approach depends on the format of your third-party tax provider.
// The current script only sets the tax rate of the sales order and does not change the taxable
// status of either the customer or any of the items in the order. But, even though the taxable
// flags are not overridden by this sample script, you can override these settings if necessary.
 
   var taxRate = body; 

        
          // Note here not to use a field-changed event. Firing a field-changed
//event may cause an infinite loop since a change to tax-rate will alter itself.    

nlapiSetFieldValue('taxrate', taxRate, false /*fire field change*/, true /*synchronous*/);
}
// END THIRD-PARTY TAX 

        

Deploy Your Script to the Shopping Cart

NetSuite recommends that you test your script thoroughly before running it in the shopping cart. You must follow a series of steps to load the script into your NetSuite account, and then run the script for testing purposes.

For details, see Deploying and Running Scriptable Cart.

Related Topics

General Notices