From 45f2347f0c7830f7b12e08d0291025d429fd6173 Mon Sep 17 00:00:00 2001 From: vijaykumar7177 <68215714+vijaykumar7177@users.noreply.github.com> Date: Sat, 18 Oct 2025 17:12:16 +0530 Subject: [PATCH 1/2] Create readme.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This script is designed to automatically convert the value of the Annual Budget field (annual_budget) from its original currency to USD. It uses the fx_currency and fx_rate tables to fetch the latest exchange rates and performs the conversion only when valid data is available. 🔍 Key Features: Field Focus: Converts the annual_budget field based on the currency specified in budget_currency. Validation: Ensures both the currency code and amount are valid before proceeding. Currency Check: If the currency is already USD, it bypasses conversion. Exchange Rate Lookup: Retrieves the most recent exchange rates for both the source currency and USD. Conversion Logic: Applies the formula USD Amount=(Original AmountSource Rate)×USD Rate\text{USD Amount} = \left(\frac{\text{Original Amount}}{\text{Source Rate}}\right) \times \text{USD Rate}USD Amount=(Source RateOriginal Amount​)×USD Rate. Error Handling: Clears the USD field if any required data is missing or invalid. This script ensures accurate and up-to-date currency conversion for budgeting purposes and is well-commented for maintainability and clarity --- .../Business Rules/Currency conversion to USD/readme.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Server-Side Components/Business Rules/Currency conversion to USD/readme.md diff --git a/Server-Side Components/Business Rules/Currency conversion to USD/readme.md b/Server-Side Components/Business Rules/Currency conversion to USD/readme.md new file mode 100644 index 0000000000..02220f25ed --- /dev/null +++ b/Server-Side Components/Business Rules/Currency conversion to USD/readme.md @@ -0,0 +1,5 @@ +This script is designed to automatically convert the value of the Annual Budget field (annual_budget) from its original currency to USD. It uses the fx_currency and fx_rate tables to fetch the latest exchange rates and performs the conversion only when valid data is available. 🔍 Key Features: + +Field Focus: Converts the annual_budget field based on the currency specified in budget_currency. Validation: Ensures both the currency code and amount are valid before proceeding. Currency Check: If the currency is already USD, it bypasses conversion. Exchange Rate Lookup: Retrieves the most recent exchange rates for both the source currency and USD. Conversion Logic: Applies the formula USD Amount=(Original AmountSource Rate)×USD Rate\text{USD Amount} = \left(\frac{\text{Original Amount}}{\text{Source Rate}}\right) \times \text{USD Rate}USD Amount=(Source RateOriginal Amount​)×USD Rate. Error Handling: Clears the USD field if any required data is missing or invalid. + +This script ensures accurate and up-to-date currency conversion for budgeting purposes and is well-commented for maintainability and clarity From 772a5cf1c48690bffcf188727d7a4d52574c6590 Mon Sep 17 00:00:00 2001 From: vijaykumar7177 <68215714+vijaykumar7177@users.noreply.github.com> Date: Sat, 18 Oct 2025 17:13:01 +0530 Subject: [PATCH 2/2] Create currency conversion usd.js --- .../currency conversion usd.js | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Server-Side Components/Business Rules/Currency conversion to USD/currency conversion usd.js diff --git a/Server-Side Components/Business Rules/Currency conversion to USD/currency conversion usd.js b/Server-Side Components/Business Rules/Currency conversion to USD/currency conversion usd.js new file mode 100644 index 0000000000..27fa909e3b --- /dev/null +++ b/Server-Side Components/Business Rules/Currency conversion to USD/currency conversion usd.js @@ -0,0 +1,72 @@ +(function executeRule(current, previous /*null when async*/) { + + // Extract the first 3 characters of the budget currency code (e.g., "INR", "EUR") + var currencyCode = current.budget_currency ? current.budget_currency.toString().substring(0, 3) : ''; + + // Convert the annual budget value to a float + var amount = parseFloat(current.annual_budget);//annual_budget is filed where we can enter amount + + // Validate input: If currency code is missing or amount is not a valid number, clear the USD field and exit + if (!currencyCode || isNaN(amount)) { + current.u_annual_budget_usd = ''; + return; + } + + // If the currency is already USD, no conversion needed — store the original amount + if (currencyCode === 'USD') { + current.u_annual_budget_usd = amount; + return; + } + + // Check if the currency exists in the fx_currency table + var currencyGR = new GlideRecord('fx_currency'); + currencyGR.addQuery('code', currencyCode); + currencyGR.query(); + + // If currency is not found, clear the USD field and exit + if (!currencyGR.next()) { + current.u_annual_budget_usd = ''; + return; + } + + // Get the latest exchange rate for the selected currency from fx_rate table + var fxGR = new GlideRecord('fx_rate'); + fxGR.addQuery('currency.code', currencyCode); + fxGR.orderByDesc('sys_updated_on'); // Sort by most recent update + fxGR.setLimit(1); // Limit to the latest record + fxGR.query(); + + // If no exchange rate found, clear the USD field and exit + if (!fxGR.next()) { + current.u_annual_budget_usd = ''; + return; + } + + var rate = parseFloat(fxGR.getValue('rate')); // Exchange rate for selected currency + + // Get the latest exchange rate for USD from fx_rate table + var fxGR1 = new GlideRecord('fx_rate'); + fxGR1.addQuery('currency.code', 'USD'); + fxGR1.orderByDesc('sys_updated_on'); // Sort by most recent update + fxGR1.setLimit(1); // Limit to the latest record + fxGR1.query(); + + // If no USD exchange rate found, clear the USD field and exit + if (!fxGR1.next()) { + current.u_annual_budget_usd = ''; + return; + } + + var usdRate = parseFloat(fxGR1.getValue('rate')); // USD base rate + + // Perform conversion only if both rates are valid and non-zero + if (!isNaN(rate) && !isNaN(usdRate) && rate !== 0) { + var convertedAmount = (amount / rate) * usdRate; // Convert to USD + current.u_annual_budget_usd = convertedAmount; // Store the converted value + } else { + gs.info("Invalid exchange rate values"); + current.u_annual_budget_usd = ''; + } + +})(current, previous); +``