In Dynamics AX and Dynamics 365 Finance and Operations (D365FO), handling multiple currencies is a common requirement for businesses operating in international markets. This capability allows organizations to conduct transactions, maintain financial records, and generate reports in various currencies while ensuring consistency and accuracy.
The provided code snippet demonstrates how to perform currency conversion using the CurrencyExchangeHelper class in X++. This example converts an amount from one currency to another using the current exchange rates defined in the system.
...
static void main(Args args)
{
CurrencyExchangeHelper exchangeHelper; // Instance of
CurrencyExchangeHelper
AmountMst
totalAmountMst; // Variable to hold the amount in main currency
CurrencyCode
destinationCurrency = 'INR'; // Target currency code
CurrencyCode
sourceCurrency = 'GBP'; // Source currency code
AmountCur
initialAmount = 10000; // Initial amount in source currency
// Initialize the
CurrencyExchangeHelper with current ledger and system date
exchangeHelper =
CurrencyExchangeHelper::newExchangeDate(Ledger::current(), systemDateGet());
// Calculate the
amount in the destination currency
totalAmountMst =
exchangeHelper.calculateCurrencyToCurrency(destinationCurrency, sourceCurrency,
initialAmount, true);
// Display the
converted amount in the Infolog
info(strFmt('%1',
totalAmountMst));
}
...
Explanation:
CurrencyExchangeHelper exchangeHelper;: Declares a variable exchangeHelper of type CurrencyExchangeHelper which is used to perform currency conversion operations.
AmountMst totalAmountMst;: Declares a variable totalAmountMst of type AmountMst which will store the converted amount in the company's main currency.
CurrencyCode destinationCurrency = 'INR';: Sets destinationCurrency to 'INR' (Indian Rupee), which is the target currency for conversion.
CurrencyCode sourceCurrency = 'GBP';: Sets sourceCurrency to 'GBP' (British Pound), which is the currency from which conversion will be done.
AmountCur initialAmount = 10000;: Sets initialAmount to 10000, which is the amount in the source currency that you want to convert.
exchangeHelper = CurrencyExchangeHelper::newExchangeDate(Ledger::current(), systemDateGet());: Initializes the exchangeHelper instance with the current ledger and system date to ensure that the exchange rates are up-to-date.
totalAmountMst = exchangeHelper.calculateCurrencyToCurrency(destinationCurrency, sourceCurrency, initialAmount, true);: Uses the exchangeHelper to calculate the equivalent amount in the target currency (INR) from the source currency (GBP). The true parameter indicates that you want to use the system currency for conversion.
info(strFmt('%1', totalAmountMst));: Displays the converted amount (totalAmountMst) in the Infolog, allowing you to verify the conversion result.
This modified code performs currency conversion from GBP to INR for an amount of 10,000 using the current ledger and system date for accurate exchange rates.