简体   繁体   中英

Trigger Avalara to run after Suitescript completes

With Colorado adding a new deliver fee tax, I am in search of a method to call the Avalara recalculation process after updating an invoice in NetSuite. We have to add a new line item associated with this tax and then have it reprocess the Avalara functions to be able to include the new line on the Avalara tax. I have completed a simple workflow to add the new line. However, it does not recall the Avalara scripts, thus, not adding the information to the taxing file. Has anyone been able to call Avalara scripts in NetSuite post workflow or script completions?

Thanks in advance,

Brad

I spent far too much time dealing with this exact issue last week. I tried to go the workflow route as well but ultimately decided a script would be needed. I tried a few methods, but the ultimate solution was to create a non-fulfillable, non-inventory item and use a User Event script deployed on the Sales Orders to add the RDF item to applicable records when they are created. In our case, these SOs are created in the UI, from a CSV import, or imported from our Shopify Webstore through a Suitelet (and Celigo). The basis of the script is as follows:

    const beforeSubmit = (context) => {

        let salesOrder = context.newRecord;
        let shipState = salesOrder.getValue('shipstate');
        let channel = salesOrder.getValue('class');
        let total = salesOrder.getValue('total');
        let customerId = salesOrder.getValue('entity');

        if (!rdfExists(salesOrder) && shipState == 'CO' && (channel == '5' || channel == '6') || (channel == '15' && total > 0)) {
            if (customerTaxable(customerId)) {
                addRDF(salesOrder);
            }
        }
    }

Essentially this is just checking some parameters (that the SO doesn't already have a RDF line item somehow, that the Ship State is CO, and that it is one of our custom channels that meets the criteria for these orders. If the order meets those criteria it will load the Customer record to check if the customer is taxable. And if they are taxable, it adds the RDF line item to the record.

This obviously includes some helper functions, which I will include snippets of, but take them with a grain of salt, we are in a highly customized instance and what works for us may not work for you (also I'm not a professional developer and this was written for completeness, not optimization).

    function rdfExists(record) {

        //Loop through line items looking for 'Colorado Retail Delivery Fee' item
        let numLines = record.getLineCount('item');
        const RDFITEM = runtime.getCurrentScript().getParameter('custscript_co_rdf_id');

        for (let i = 0; i < numLines; i++) {
            let itemId = record.getSublistValue({
                sublistId: 'item',
                fieldId: 'item',
                line: i
            });

            if (itemId == RDFITEM) {
                return true;
            }
        }
        return false;
    }

    function addRDF(record) {

        const RDFITEM = runtime.getCurrentScript().getParameter('custscript_co_rdf_id');
        let numLines = record.getLineCount('item');

        record.insertLine({
            sublistId: 'item',
            line: numLines,
            ignoreRecalc: false
        });

        record.setSublistValue({
            sublistId: 'item',
            fieldId: 'item',
            line: numLines,
            value: RDFITEM
        });

        record.setSublistValue({
            sublistId: 'item',
            fieldId: 'quantity',
            line: numLines,
            value: 1
        });

        record.setSublistValue({
            sublistId: 'item',
            fieldId: 'taxcode',
            line: numLines,
            value: '5025'
        });
    }

    function customerTaxable(customerId) {

        let customerRecord = record.load({
            type: record.Type.CUSTOMER,
            id: customerId
        });

        if (customerRecord.getValue('taxable')) {
            return true;
        } else {
            return false;
        }
    }

This successfully added the line to the Sales Orders but was still not calling Avalara to calculate the tax on the new item. To accomplish this, there were a few more steps:

  • Set the item's Avatax Tax Code to OF400000 (per Avalara help article here )
  • Do essentially the opposite of this Avalara help article and move the AVA_TransactionTab_2 UE script to the bottom of the Scripted Record list (Avalara says it goes at the bottom, but ours was first for some reason).
  • Modify the file /SuiteBundles/Bundle 1894/utility/AVA_TaxLibrary.js to allow for calculation of taxes on the new RDF item. This is the part I'm least excited about. I tried a ton of things, but the tax wouldn't calculate. Very long story short the line item was "failing" the AVA_MainTaxCodeCheck function that was called in the AVA_RequiredFieldsFunction that was called in the AVA_TransactionBeforeSubmit function and therefore not running the AVA_CalculateTax function... confused yet? Basically I just modified that library to include a statement that if it failed to find the lineTaxCode for an item, to check if it was my RDF Item, and if it was to calculate tax regardless (the line item is only added to the orders I want it to taxed on). I don't want to share my hardcoding of that library in public, but message me if you want more info.

From there we modified the SO and Invoice PDF templates to include logic to remove the $.27 from the tax line on these orders and add it as it's own line item:

CO RDF 发票行项目

All in all - it's a solution that works for us. Obviously your buisness case will be different, but this is what worked for us, and hopefully can at least steer you in the right direction. All this for 27 cents...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM