On Vendor Bill record Creation, the script will fetch the Tax Amount(VAT AMT) and it will be added to the corresponding Line Amount(AMOUNT) field. Also, it will update the Tax code(TAX CODE) on each line in the Item sublist to a Tax code with a 0 % value.
UserEventScript
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
/****************************************************************************
* Macrofin - ZEGO
* MAC-117 Zego - AP VAT Requirement
* **************************************************************************
* Date: 02/04/2020
*
* Author: Jobin & Jismi IT Services LLP
* Script Description : On Vendor Bill record Creation, script will fetch the Tax Amount
(VAT AMT) and it will be added to the corresponding Line Amount(AMOUNT) field. Also it will update the Tax code(TAX CODE) on each line in the Item sublist to a Tax code with a 0 % value.
* Date created : 02 April 2020
*
* REVISION HISTORY
*
* Revision 1.0 02/04/2020 md: Create
* Update 1.1 29/05/2020 md: Added expense sublist. Each item and expense sublist line will be updated only if
* 1. The line Tax code on the line is a UK tax code.
* 2. If the field “REVERSE CHARGE CODE”(of UK tax code) in the tax code record is not checked.
*
****************************************************************************/
define(['N/record', 'N/search'],
function (record, search) {
function afterSubmit(scriptContext) {
try {
var vendorBillRecId = scriptContext.newRecord.id;
if (scriptContext.type == 'create') {
var vendorBillRec = record.load({ type: record.Type.VENDOR_BILL, id: vendorBillRecId, isDynamic: true });
var VendorLineCount = vendorBillRec.getLineCount({ sublistId: 'item' });
var VendorLineExpenseCount = vendorBillRec.getLineCount({ sublistId: 'expense' });
var vendorBillRecSubsidiary = vendorBillRec.getValue({ fieldId: 'subsidiary' });
//log.debug("VendorLineCount", VendorLineCount);
var subsidaryStateObj = { "France": 59, "Ireland": 21, "Spain": 38, "United Kingdom": 5, "Gibraltar": 5 };
var SubsidiaryRec = search.lookupFields({
type: search.Type.SUBSIDIARY,
id: vendorBillRecSubsidiary,
columns: ['country']
});
var SubsidCountry = SubsidiaryRec.country[0].text;
var taxCode = subsidaryStateObj[SubsidCountry];
log.debug("taxCode", taxCode);
//Updating amount and tax code for Item sublist
for (var i = 0; i < VendorLineCount; i++) {
vendorBillRec.selectLine({ sublistId: 'item', line: i });
var taxcodeVal = vendorBillRec.getCurrentSublistValue({ sublistId: 'item', fieldId: 'taxcode', line: i });
log.debug("taxcodeVal", taxcodeVal);
//Search to check whether the tax code country is United Kingdom and Reverse Charge Code is False.
var salestaxitemSearchObj = search.create({
type: "salestaxitem",
filters: [
["internalid", "anyof", taxcodeVal],
"AND",
["country", "anyof", "GB"],
"AND",
["isreversecharge","is","F"]
],
columns: [
search.createColumn({ name: "itemid", label: "Item ID" })
]
});
var searchResultCount = salestaxitemSearchObj.runPaged().count;
if (searchResultCount > 0) {
var ItemTaxAmount = vendorBillRec.getCurrentSublistValue({ sublistId: 'item', fieldId: 'tax1amt', line: i });
var ItemAmount = vendorBillRec.getCurrentSublistValue({ sublistId: 'item', fieldId: 'amount', line: i });
var TotalItemAmount = ItemTaxAmount + ItemAmount;
log.debug("TotalItemAmount", TotalItemAmount);
vendorBillRec.setCurrentSublistValue({ sublistId: 'item', fieldId: 'amount', line: i, value: TotalItemAmount });
if (taxCode) {
vendorBillRec.setCurrentSublistValue({ sublistId: 'item', fieldId: 'taxcode', line: i, value: taxCode });
}
}
vendorBillRec.commitLine({ sublistId: 'item' });
}
//Updating amount and tax code for Expense sublist
for (var j = 0; j < VendorLineExpenseCount; j++) {
vendorBillRec.selectLine({ sublistId: 'expense', line: j });
var ExpensetaxcodeVal = vendorBillRec.getCurrentSublistValue({ sublistId: 'expense', fieldId: 'taxcode', line: j });
log.debug("ExpensetaxcodeVal", ExpensetaxcodeVal);
//Search to check whether the tax code country is United Kingdom and Reverse Charge Code is False.
var salestaxitemSearchObj = search.create({
type: "salestaxitem",
filters: [
["internalid", "anyof", ExpensetaxcodeVal],
"AND",
["country", "anyof", "GB"],
"AND",
["isreversecharge","is","F"]
],
columns: [
search.createColumn({ name: "itemid", label: "Item ID" })
]
});
var ExpensesearchResultCount = salestaxitemSearchObj.runPaged().count;
if (ExpensesearchResultCount > 0) {
var ExpenseTaxAmount = vendorBillRec.getCurrentSublistValue({ sublistId: 'expense', fieldId: 'tax1amt', line: j });
var ExpenseAmount = vendorBillRec.getCurrentSublistValue({ sublistId: 'expense', fieldId: 'amount', line: j });
var TotalExpenseAmount = ExpenseTaxAmount + ExpenseAmount;
log.debug("TotalItemAmount", TotalItemAmount);
vendorBillRec.setCurrentSublistValue({ sublistId: 'expense', fieldId: 'amount', line: j, value: TotalExpenseAmount });
if (taxCode) {
vendorBillRec.setCurrentSublistValue({ sublistId: 'expense', fieldId: 'taxcode', line: j, value: taxCode });
}
}
vendorBillRec.commitLine({ sublistId: 'expense' });
}
vendorBillRec.save();
}
} catch (err) {
log.debug("ERROR_ON_SUBMIT", err);
log.error("ERROR_ON_SUBMIT", err);
}
}
return {
afterSubmit: afterSubmit
};
});