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

Run AX report using X++

       Args                     args;      ReportRun          report;     salesLineProductPartProd salesLineProductPartProdLocal;     ;     ...