Get the latest exchange rates in Dynamics AX 2012 Using X++
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.
Note: Please check and verify this web URL before using it [free source or not].
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 = newSystem.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> </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:
You can integrating the exchange rates in to the tablesExchangeRate , ExchangeRateType , ExchangeRateCurrencyPair
To get exchange rates for other currencies you may need to modify the URL as shown below. [Change INR to EUR etc.]
Enjoy...........
-Harry