Get the latest exchange rates in Dynamics AX 2012 [Using X++]

Friends,

Below small snippet will help you to get the latest exchange rates as on today.

I am using x-rates URL to pull the exchange rates for this example. Please check/verify the URL before using it [free source or not].

image

static void SR_getExchangeRates(Args _args)

{

    int         curPos, endPos, startPos;

    TextBuffer  tb = new TextBuffer();

    System.Net.WebRequest webRequest;

    System.Net.WebResponse webResponse;

    str page;

    System.IO.StreamReader streamReader;

 

    try

    {

        webRequest = System.Net.WebRequest::Create("http://www.x-rates.com/d/INR/table.html");

 

        // this will throw an webexception if cannot be reached.

        webResponse = webRequest.GetResponse();

        streamReader = new System.IO.StreamReader(webResponse.GetResponseStream());

 

        tb.setText();

        page = streamReader.ReadToEnd();

 

        streamReader.Close();

 

        tb.setText(page);

        curpos   = 1;

        startPos = 1;

        tb.regularExpressions(false);

 

        tb.find(‘<a href="/d/INR/USD/graph120.html" class="menu">’, curpos);

 

        startpos = tb.matchPos();

 

        tb.find(‘</a>&nbsp;</font></td>’, startpos);

        endpos = tb.matchPos();

 

        page = tb.subStr(startpos, endpos – startpos);

        info(strFmt("1 USD = %1 INR",strreplace(page,‘<a href="/d/INR/USD/graph120.html" class="menu">’,)));

 

        // Close the webResonse

        webResponse.Close();

    }

    catch(Exception::CLRError)

    {

        throw error(AifUtil::getClrErrorMessage());

    }

}

Below is the output:

image

If anyone is interested in integrating the exchange rates in to the tables ExchangeRate , ExchangeRateType , ExchangeRateCurrencyPair  

[AX 2012] , please drop me an email/message.

 

Well, to get exchange rates for other currencies you need to modify the URL  as shown below. [Change INR to EUR etc.]

 

image

Thank you to x-rates.com

 

Happy Dax6ng,

 

 sree

Advertisements

2 Responses to “Get the latest exchange rates in Dynamics AX 2012 [Using X++]”

  1. Emre TUFEKCI Says:

    Hi, and thanks for your sharing.
    I’m triyng to integrate my existing class to Ax2012 for reading ExcRates of Turkey.

    I’ve tried the code as a job and it works fine.
    But when i pu it into a class by the same way, it throws an error on the line
    webResponse = webRequest.GetResponse();

    My class is set to be worked on server, my method is static. What is missing?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: