Thursday 9 September 2021

Find Current Exchange rate using X++ / Ax

Below is the code to find the current exchange rate of given currencies in X++

static void CurrentExchangeRate(Args _args)
{
    ExchangeRate     exchangeRate;
    ExchangeRateType ExchangeRateType; 
    ExchangeRateCurrencyPair exchangeRateCurrencyPair;
    real             exchRate;
    
    CurrencyCode fromCurrency  = "USD";
    CurrencyCode toCurrency    = "PKR";
    TransDate    transDate     = today();
    
    select firstonly exchangeRateCurrencyPair 
        where 
        exchangeRateCurrencyPair.ExchangeRateType 
        == Ledger::find(Ledger::current()).DefaultExchangeRateType
        &&  exchangeRateCurrencyPair.FromCurrencyCode == fromCurrency 
        &&  exchangeRateCurrencyPair.ToCurrencyCode   == toCurrency;

        exchRate = exchangeRate::findByDate(exchangeRateCurrencyPair.RecId,
            transDate).ExchangeRate;
    
    info(strFmt("Exchange Rate = %1",exchRate/100));
    
}

No comments:

Post a Comment

Difference Between Edit And Display Method in Ax

Display Method: The display method means that the method’s return value is being displayed on a form or a report.  This value is fixed and c...