How to get exchange rates from European Central Bank for non euro countries in Business Central

For the second time already I have been asked to come up with a solution in Business Central to get the exchange rates for non euro country from the European Central Bank. The reason is that if you have multiple companies then you want to have the same currency rates in all the companies coming from the same source, for example European Central Bank.

The trick here is that ECB issues all their exchange rates against EUR and if you need to get exchange rates in a country without EUR as local currency, for example Norway or the UK, then you don’t get the EUR against your local currency from ECB.

Here’s a small solution that resolves it.

codeunit 50106 "ECB Currency Rate"
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Map Currency Exchange Rate", 'OnBeforeInsertOrModifyRecordRef', '', false, false)]
    local procedure GetEurRateFromLocalCurrency(var RecordRef: RecordRef; var IsHandled: Boolean)
    var
        GenLedgerSetup: Record "General Ledger Setup";
        CurrencyExchangeRate: Record "Currency Exchange Rate";
        ExchangeRate: Decimal;
    begin
        GenLedgerSetup.SetLoadFields("LCY Code");
        if GenLedgerSetup.Get() then begin
            if GenLedgerSetup."LCY Code" = Format(RecordRef.Field(1).Value()) then begin
                RecordRef.Field(1).Value := 'EUR';
                ExchangeRate := RecordRef.Field(3).Value();
                if ExchangeRate <> 0 then begin
                    RecordRef.Field(3).Value := 1 / ExchangeRate;
                    RecordRef.Field(4).Value := 1 / ExchangeRate;
                end;
            end;
        end;
    end;
}

What this code snippet does is that it will compare the exchange rate from ECB against the local currency in your company and if they match then it will calculate the rate from the local currency rate that you have received from ECB.

This solution will work only then if your local currency is one published by the ECB.

Now we have the correct EUR rate coming from ECB in a company that uses GBP as the local currency.