Wednesday, 4 March 2015

Getting exchange rate value in ax 2012

public static CurrencyExchangeRate currencyConversion(CurrencyCode fromCurrencyCode, CurrencyCode toCurrencyCode)
{
    ExchangeRateCurrencyPair exchangeRateCurrencyPair,exchangeRateCurrencyPair2;
    ExchangeRateDisplayFactor exchangeRateDisplayFactor;
    ExchangeRate exchangeRate;
    date validFromDate = dateNull();
    date validToDate = dateMax();


    CurrencyCode _fromCurrencyCode =  fromCurrencyCode;
    CurrencyCode _toCurrencyCode = toCurrencyCode;
    ExchangeRateTypeRecId _exchangeRateType2;
    boolean _getReciprocalIfPrimaryNotFound = true;

    CurrencyExchangeRate exchRateValue;

    _exchangeRateType2 = ExchangeRateType::findByName(Exchange Rate Type).RecId;


    select firstonly ExchangeRateDisplayFactor
        from exchangeRateCurrencyPair
        where exchangeRateCurrencyPair.FromCurrencyCode == _fromCurrencyCode
            && exchangeRateCurrencyPair.ToCurrencyCode == _toCurrencyCode
            && exchangeRateCurrencyPair.ExchangeRateType == _exchangeRateType2;
    if(exchangeRateCurrencyPair.ExchangeRateDisplayFactor)
    {

        select firstonly validtimestate(validFromDate, validToDate) exchangeRate
        where
        exchangeRate.ExchangeRateCurrencyPair == exchangeRateCurrencyPair.RecId;
        exchRateValue = ExchangeRateHelper::displayStoredExchangeRate_Static(
                exchangeRate.ExchangeRate,
                exchangeRateCurrencyPair.ExchangeRateDisplayFactor);
    }
    else
    {
       select firstonly ExchangeRateDisplayFactor
        from exchangeRateCurrencyPair2
        where exchangeRateCurrencyPair2.FromCurrencyCode == _toCurrencyCode
            && exchangeRateCurrencyPair2.ToCurrencyCode == _fromCurrencyCode
            && exchangeRateCurrencyPair2.ExchangeRateType == _exchangeRateType2;

        if(exchangeRateCurrencyPair2.exchangeRateDisplayFactor)
        {
            select firstonly validtimestate(validFromDate, validToDate) exchangeRate
            where exchangeRate.ExchangeRateCurrencyPair == exchangeRateCurrencyPair2.RecId;

            exchRateValue = exchangeRateCurrencyPair2.ExchangeRateDisplayFactor / ExchangeRateHelper::displayStoredExchangeRate_Static(
                exchangeRate.ExchangeRate,
                exchangeRateCurrencyPair2.ExchangeRateDisplayFactor);
        }
    }
    return exchRateValue;
}

1 comment:

Calculate ledger balance by dimension set in X++ in AX2012/Dynamics 365 FO

There are a variety of ways users can view balances in the general ledger. Some of the most common options are: 1. Trial balance 2. Financia...