Skip to main content
X++ Development · · 1 min read

Get Exchange Rate for Currency Conversion

Retrieve exchange rates in D365 F&O using X++ for currency conversion and exchange rate-based financial calculations.

Ahmad Hassan

Ahmad Hassan

Dynamics 365 F&O Consultant

Share
Get Exchange Rate for Currency Conversion

This method retrieves the exchange rate for the current record’s currency against USD based on the invoice date. It identifies the applicable exchange rate type from the current ledger and finds the matching currency pair. If a valid currency pair exists, the exchange rate for the specified date is returned after conversion from percentage format; otherwise, it returns 0.

X++ Code

display Real exchangeRateFormatted()
    {
        ExchangeRate                exchangeRate;
        ExchangeRateCurrencyPair    exchangeRateCurrencyPair;
        CurrencyCode                fromCurrency  = this.CurrencyCode;
        CurrencyCode                toCurrency    = "USD"; //Ledger::accountingCurrency();
        TransDate                   transDate     = this.InvoiceDate;
        Real                        exchRate;

        select firstonly exchangeRateCurrencyPair
            where exchangeRateCurrencyPair.ExchangeRateType == Ledger::find(Ledger::current()).DefaultExchangeRateType
&& exchangeRateCurrencyPair.FromCurrencyCode == fromCurrency
&& exchangeRateCurrencyPair.ToCurrencyCode   == toCurrency;

        if (exchangeRateCurrencyPair.RecId)
        {
            exchRate = exchangeRate::findByDate(exchangeRateCurrencyPair.RecId, transDate).ExchangeRate;
            return exchRate/100;
        }

        return 0;
    }

Process Flow

The display method gets the source currency from CurrencyCode, sets USD as the target currency, and uses InvoiceDate for the exchange-rate lookup.

It identifies the current ledger's default exchange rate type and searches for the matching currency pair.

If the currency pair exists, the exchange rate for the invoice date is retrieved and divided by 100.

If no matching currency pair is found, the method returns 0.

0fce1d1f-50ea-41b7-ba61-e79e2e6a0cb3.png

Figure: Flow of retrieving and formatting the invoice currency exchange rate against USD.

Found this useful?

Share it with your network

Ahmad Hassan

Written by

Ahmad Hassan

Microsoft Dynamics 365 & Power Platform blogger.

More about me

Related Posts

Comments

Leave a comment