web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

Import Exchange Rates from Central European Bank

Roberto Stefanetti Profile Picture Roberto Stefanetti 12,998

Codeunit Import Exchange Rates

A very simply codeunit to import daily exchange rates directly from Central European Bank with XML Filestream IN


Documentation Section

//*** Import "New Change Rate" from  "ECB.EUROPA.EU"    --> FIX TIME >= 2.00 AM
//*** Import XML File "Change Rate" from "ECB.EUROPA.EU" --> "eurofxref-daily.xml"
//*** XmlTextReader Global Dim  DotNet  System.Xml.XmlTextReader.'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
            
Global Variables
XmlTextReader DotNet  System.Xml.XmlTextReader.'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 
codCurrency Code 20
decRate  Decimal  
datValueDate  Date  
intYear  Integer  
intMonth  Integer  
intDay  Integer  
intNodeCounter  Integer  
Currency Record  Currency 
CurrencyExchangeRate Record  Currency Exchange Rate 


OnRun() Section

//Direct XML Text Reader (XML Stream IN)
XmlTextReader := XmlTextReader.XmlTextReader('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
 
//LOOP XML STREAMING IN
WHILE XmlTextReader.Read DO BEGIN
  IF XmlTextReader.Name = 'Cube' THEN BEGIN
    IF XmlTextReader.AttributeCount = 1 THEN BEGIN
      XmlTextReader.MoveToAttribute('time');  //Time
      EVALUATE(intYear,COPYSTR(XmlTextReader.Value,1,4));   //Year
      EVALUATE(intMonth,COPYSTR(XmlTextReader.Value,6,2));  //Month
      EVALUATE(intDay,COPYSTR(XmlTextReader.Value,9,2));    //Day
      datValueDate := DMY2DATE(intDay,intMonth,intYear);    //Value Date
    END;
 
    IF XmlTextReader.AttributeCount = 2 THEN BEGIN
      XmlTextReader.MoveToAttribute('currency');  // Currency
      codCurrency := XmlTextReader.Value;     
      XmlTextReader.MoveToAttribute('rate');   // Rate
      EVALUATE(decRate,CONVERTSTR(XmlTextReader.Value,'.',',')); 

      //  MESSAGE('%1 - %2',codCurrency,decRate); > TEST MESSAGE
           
      // INSERT\MODIFY New Fix Exchange Rate
      IF Currency.GET(codCurrency) THEN BEGIN
        CurrencyExchangeRate."Currency Code" := codCurrency;
        CurrencyExchangeRate."Starting Date" := WORKDATE;
        CurrencyExchangeRate."Exchange Rate Amount" :=  decRate;
        CurrencyExchangeRate."Relational Exch. Rate Amount" :=1;
        CurrencyExchangeRate."Fix Exchange Rate Amount" := CurrencyExchangeRate."Fix Exchange Rate Amount"::"Relational Currency";
        IF NOT CurrencyExchangeRate.INSERT THEN
           CurrencyExchangeRate.MODIFY;
      END;    
    END;  
  END;
END;

Based and rivisited on original Mibuso solution.


This was originally posted here.

Comments

*This post is locked for comments