Introduction
In the evolving landscape of financial technology, seamless integration and connectivity between financial institutions and various accounting systems is paramount. One such enhancement is the ability to manage multiple accounts within the Financial Institution (FI) plugin. This article delves into the implementation of adding multiple accounts in a NetSuite Financial Institution Connectivity Plugin using SuiteScript 2.x. We’ll walk through the script structure, highlighting key functions and how they contribute to achieving the desired functionality.
Script Overview
The Financial Institution Connectivity Plugin facilitates interactions between NetSuite and external financial institutions. By extending this plugin to support multiple accounts, businesses can streamline their financial data management more efficiently.
Script Structure
The script is divided into two primary functions:
- getAccounts(context): Retrieves and configures the accounts.
- getTransactionData(context): Fetches transaction data for the configured accounts.
Here is the complete implementation:
/**
* @NApiVersion 2.x
* @NScriptType fiConnectivityPlugin
* @NModuleScope SameAccount
*/
define(['N/search'], function (search) {
function getAccounts(context) {
// Get the current date and time
var currentDate = new Date();
// Extract the components of the current date and time
var year = currentDate.getFullYear();
var month = (currentDate.getMonth() + 1 < 10 ? '0' : '') + (currentDate.getMonth() + 1); // Months are zero-indexed
var day = (currentDate.getDate() < 10 ? '0' : '') + currentDate.getDate();
var hours = (currentDate.getHours() < 10 ? '0' : '') + currentDate.getHours();
var minutes = (currentDate.getMinutes() < 10 ? '0' : '') + currentDate.getMinutes();
var seconds = (currentDate.getSeconds() < 10 ? '0' : '') + currentDate.getSeconds();
// Format the date and time string
var formattedDateTime = year + '-' + month + '-' + day + 'T' + hours + ':' + minutes + ':' + seconds;
// Retrieve configuration ID from the plugin configuration
var configurationId = context.pluginConfiguration.getConfigurationFieldValue({ fieldName: "configuration_id" });
log.debug("Configuration ID", configurationId);
// Adding multiple accounts
context.addAccount({
accountMappingKey: "449123",
displayName: "HDFC Bank",
accountType: "BANK",
currency: "USD",
groupName: "Bank of India",
lastUpdated: formattedDateTime
});
context.addAccount({
accountMappingKey: "11111",
displayName: "ICICI Bank",
accountType: "BANK",
currency: "INR",
groupName: "Bank of India",
lastUpdated: formattedDateTime
});
}
function getTransactionData(context) {
var configurationId = context.pluginConfiguration.getConfigurationFieldValue({ fieldName: "configuration_id" });
// Parsing account requests
var accountRequests = JSON.parse(context.accountRequestsJSON);
log.debug("Account Requests", accountRequests);
// Data for ICICI Bank
var iciciObj = {
"accountId": "",
"dataAsOfDate": "2024-01-01",
"closingBalance": 200.0,
"transactions": [
{
"date": "2024-02-26",
"amount": 250.0,
"transactionTypeCode": "OTHER",
"uniqueId": "1141",
"id": "CHK0012",
"payee": "TEST1",
"currency": "INR",
"memo": "INV 3",
"transactionStatus": "Posted"
},
{
"date": "2024-01-01",
"amount": 200.0,
"transactionTypeCode": "OTHER",
"uniqueId": "1257",
"id": "CHK0012",
"payee": "TEST2",
"currency": "INR",
"memo": "INV 3",
"transactionStatus": "Posted"
}
]
};
// Data for HDFC Bank
var hdfcObject = {
"accountId": "",
"dataAsOfDate": "2024-02-01",
"closingBalance": 400.0,
"transactions": [
{
"date": "2024-03-26",
"amount": 150.0,
"transactionTypeCode": "OTHER",
"uniqueId": "1141",
"id": "CHK0012",
"payee": "TEST1",
"currency": "USD",
"memo": "INV 4",
"transactionStatus": "Posted"
},
{
"date": "2024-02-01",
"amount": 400.0,
"transactionTypeCode": "OTHER",
"uniqueId": "1257",
"id": "CHK0012",
"payee": "TEST2",
"currency": "USD",
"memo": "INV 2",
"transactionStatus": "Posted"
}
]
};
// Object to hold downloaded data
var downloadedData = {
"accounts": []
};
// Check for account requests and process accordingly
if (accountRequests != null) {
accountRequests.forEach(function (accountRequest) {
var accountId = accountRequest.accountMappingKey;
var fromDateTime = accountRequest.dataStartTime;
var toDateTime = accountRequest.dataEndTime;
// Assigning account IDs and adding to the downloaded data
if (accountId == "449123") {
hdfcObject["accountId"] = accountId;
downloadedData["accounts"].push(hdfcObject);
} else if (accountId == "11111") {
iciciObj["accountId"] = accountId;
downloadedData["accounts"].push(iciciObj);
}
});
}
log.debug("Downloaded Data", downloadedData);
// Adding the data chunk to the context
context.addDataChunk({ dataChunk: JSON.stringify(downloadedData) });
// Returning account requests JSON
context.returnAccountRequestsJSON({ accountsJson: context.accountRequestsJSON });
log.debug("Written to DB");
}
return {
getAccounts: getAccounts,
getTransactionData: getTransactionData
}
});
getAccounts(context)
This function sets up the accounts within the plugin. Here’s a breakdown of its operations:
- Current Date and Time:
- The script gets the current date and time, formats it, and uses this timestamp to indicate when the account details were last updated.
- Configuration ID:
- Retrieves the configuration ID from the plugin’s configuration settings.
- Adding Accounts:
- Adds multiple accounts to the context with details like accountMappingKey, displayName, accountType, currency, groupName, and lastUpdated timestamp.
getTransactionData(context)
This function fetches transaction data for the configured accounts. Here’s a breakdown:
- Configuration ID:
- Retrieves the configuration ID from the plugin’s configuration settings.
- Account Requests Parsing:
- Parses the account requests from the context to fetch transactions for each account.
- Transaction Data Objects:
- Defines transaction data for multiple accounts (HDFC Bank and ICICI Bank).
- Downloaded Data:
- Constructs the
downloadedDataobject with account and transaction details based on the account requests.
- Adding Data Chunk:
- Adds the constructed data chunk to the context and returns the account requests JSON.
Conclusion
Enhancing the Financial Institution Connectivity Plugin to support multiple accounts significantly improves its utility and efficiency. The provided script ensures seamless integration and accurate data handling for multiple financial accounts, facilitating better financial management and reporting. This implementation demonstrates the power and flexibility of SuiteScript 2.x in customizing and extending NetSuite’s capabilities to meet specific business needs.