Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
``
Original file line number Diff line number Diff line change
@@ -1,12 +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:
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.
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.
This script ensures accurate and up-to-date currency conversion for budgeting purposes and is well-commented for maintainability and clarity
Loading