Setting VAT Number to the Custom Field

Jira Code: FHI-5

Based on the First Line Item Tax Code, we will make a custom record search and get the VAT number. And set that VAT number to the custom field of the transaction record.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************
 * FHI-5 VAT NUMBER TAX CODE 
 * *************************************************************************
 * 
 * Date: 05-03-2019
 * 
 * Updated Date :
 * 
 * Author: Jobin & Jismi IT Services LLP
 * 
 *****************************************************************************
 **/
define(['N/file', 'N/url', 'N/search', 'N/runtime', 'N/record', 'N/https', 'N/ui/serverWidget', 'N/email', 'N/task', 'N/format'],

    function (file, url, search, runtime, record, https, serverWidget, task, format) {
        var main = {
            beforeSubmit: function (scriptContext) {
                var record = scriptContext.newRecord;
                var nameoftaxrecord;
                var taxcodeofrecord;
                var eu_vat_ofrecord;
                var tax_code = record.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'taxcode',
                    line: 0
                });
                log.debug("tax_code", tax_code)
                var customrecord_eu_vat_numberSearchObj = search.create({
                    type: "customrecord_eu_vat_number",
                    filters: [
                        ["custrecord_tax_code", "anyof", tax_code]
                    ],
                    columns: [
                        search.createColumn({
                            name: "name",
                            sort: search.Sort.ASC,
                            label: "Name"
                        }),
                        search.createColumn({
                            name: "custrecord_tax_code",
                            label: "Tax Code"
                        }),
                        search.createColumn({
                            name: "custrecord_eu_vat_number",
                            label: "EU VAT Number"
                        })
                    ]
                });
                var searchResultCount = customrecord_eu_vat_numberSearchObj.runPaged().count;
                log.debug("customrecord_eu_vat_numberSearchObj result count", searchResultCount);
                customrecord_eu_vat_numberSearchObj.run().each(function (result) {
                    nameoftaxrecord = result.getValue({
                        name: "name",
                        sort: search.Sort.ASC,
                        label: "Name"
                    });
                    taxcodeofrecord = result.getText({
                        name: "custrecord_tax_code",
                        label: "Tax Code"
                    });
                    eu_vat_ofrecord = result.getValue({
                        name: "custrecord_eu_vat_number",
                        label: "EU VAT Number"
                    });
                });
                if (eu_vat_ofrecord == null || eu_vat_ofrecord == undefined || eu_vat_ofrecord==""){

                }else{
                record.setValue({
                    fieldId: 'custbody_jj_taxcode_vatnumber',
                    value: nameoftaxrecord + " | " + taxcodeofrecord + " | " + eu_vat_ofrecord
                });
            }
            }

        };
        for (var key in main) {
            if (typeof main[key] === 'function') {
                main[key] = trycatch(main[key], key);
            }
        }

        function trycatch(myfunction, key) {
            return function () {
                try {
                    return myfunction.apply(this, arguments);
                } catch (e) {
                    log.debug("e in  " + key, e);
                }
            }
        };
        return main;
    });

Leave a comment

Your email address will not be published. Required fields are marked *