Skip to content

Commit 64d7dde

Browse files
Currency conversion (#2258)
* Create readme.md 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 * Create currency conversion usd.js
1 parent ef1d3fd commit 64d7dde

File tree

2 files changed

+75
-10
lines changed

2 files changed

+75
-10
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
(function executeRule(current, previous /*null when async*/) {
2+
3+
// Extract the first 3 characters of the budget currency code (e.g., "INR", "EUR")
4+
var currencyCode = current.budget_currency ? current.budget_currency.toString().substring(0, 3) : '';
5+
6+
// Convert the annual budget value to a float
7+
var amount = parseFloat(current.annual_budget);//annual_budget is filed where we can enter amount
8+
9+
// Validate input: If currency code is missing or amount is not a valid number, clear the USD field and exit
10+
if (!currencyCode || isNaN(amount)) {
11+
current.u_annual_budget_usd = '';
12+
return;
13+
}
14+
15+
// If the currency is already USD, no conversion needed — store the original amount
16+
if (currencyCode === 'USD') {
17+
current.u_annual_budget_usd = amount;
18+
return;
19+
}
20+
21+
// Check if the currency exists in the fx_currency table
22+
var currencyGR = new GlideRecord('fx_currency');
23+
currencyGR.addQuery('code', currencyCode);
24+
currencyGR.query();
25+
26+
// If currency is not found, clear the USD field and exit
27+
if (!currencyGR.next()) {
28+
current.u_annual_budget_usd = '';
29+
return;
30+
}
31+
32+
// Get the latest exchange rate for the selected currency from fx_rate table
33+
var fxGR = new GlideRecord('fx_rate');
34+
fxGR.addQuery('currency.code', currencyCode);
35+
fxGR.orderByDesc('sys_updated_on'); // Sort by most recent update
36+
fxGR.setLimit(1); // Limit to the latest record
37+
fxGR.query();
38+
39+
// If no exchange rate found, clear the USD field and exit
40+
if (!fxGR.next()) {
41+
current.u_annual_budget_usd = '';
42+
return;
43+
}
44+
45+
var rate = parseFloat(fxGR.getValue('rate')); // Exchange rate for selected currency
46+
47+
// Get the latest exchange rate for USD from fx_rate table
48+
var fxGR1 = new GlideRecord('fx_rate');
49+
fxGR1.addQuery('currency.code', 'USD');
50+
fxGR1.orderByDesc('sys_updated_on'); // Sort by most recent update
51+
fxGR1.setLimit(1); // Limit to the latest record
52+
fxGR1.query();
53+
54+
// If no USD exchange rate found, clear the USD field and exit
55+
if (!fxGR1.next()) {
56+
current.u_annual_budget_usd = '';
57+
return;
58+
}
59+
60+
var usdRate = parseFloat(fxGR1.getValue('rate')); // USD base rate
61+
62+
// Perform conversion only if both rates are valid and non-zero
63+
if (!isNaN(rate) && !isNaN(usdRate) && rate !== 0) {
64+
var convertedAmount = (amount / rate) * usdRate; // Convert to USD
65+
current.u_annual_budget_usd = convertedAmount; // Store the converted value
66+
} else {
67+
gs.info("Invalid exchange rate values");
68+
current.u_annual_budget_usd = '';
69+
}
70+
71+
})(current, previous);
72+
``
Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
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.
2-
🔍 Key Features:
1+
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:
32

4-
Field Focus: Converts the annual_budget field based on the currency specified in budget_currency.
5-
Validation: Ensures both the currency code and amount are valid before proceeding.
6-
Currency Check: If the currency is already USD, it bypasses conversion.
7-
Exchange Rate Lookup: Retrieves the most recent exchange rates for both the source currency and USD.
8-
Conversion Logic: Applies the formula
9-
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
10-
Error Handling: Clears the USD field if any required data is missing or invalid.
3+
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.
114

12-
This script ensures accurate and up-to-date currency conversion for budgeting purposes and is well-commented for maintainability and clarity.
5+
This script ensures accurate and up-to-date currency conversion for budgeting purposes and is well-commented for maintainability and clarity

0 commit comments

Comments
 (0)