codeunit 50122 "CustomJSON ExcRate Service"
{
SingleInstance = false;
Subtype = Normal;
trigger OnRun()
begin
FetchMultipleExchangeRates;
Message('Exchange rates updated successfully.');
// ImportXML('https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
end;
var
gHttpClient: HttpClient;
HttpResponseMessage: HttpResponseMessage;
ResponseText: Text;
CurrencyExchangeRate: Record "Currency Exchange Rate";
BaseCurrencies: List of [Text];
TargetCurrencies: List of [Text];
ECBApiUrl: Text[250];
gXMLDocument: XmlDocument;
XmlNodeList: XmlNodeList;
gXmlNode: XmlNode;
xmlAttrCol: XmlAttributeCollection;
xmlAttr: XmlAttribute;
Rate: Decimal;
TargetCurrency: Text;
i: Integer;
retmessage: Text;
procedure FetchMultipleExchangeRates()
var
BaseCurrency: Text;
RateAttr: Text;
IsParsed: Boolean;
ECBApiUrl: Text;
HttpResponseMessage: HttpResponseMessage;
ResponseText: Text;
gXmlDocument: XmlDocument;
XmlNodeList: XmlNodeList;
gXmlNode: XmlNode; // Ensure gXmlNode is defined here
XmlElement: XmlElement;
xmlAttrCol: XmlAttributeCollection;
xmlAttr: XmlAttribute;
TargetCurrency: Code[10];
Rate: Decimal;
I: Integer;
CurrencyExchangeRate: Record "Currency Exchange Rate";
TargetCurrencies: List of [Code[10]];
BaseCurrencies: List of [Code[10]];
xmlbuff: Record "XML Buffer";
begin
// Initialize base and target currencies lists
BaseCurrencies.Add('USD');
BaseCurrencies.Add('EUR');
BaseCurrencies.Add('GBP');
BaseCurrencies.Add('PLN');
TargetCurrencies.Add('GBP');
TargetCurrencies.Add('USD');
TargetCurrencies.Add('PLN');
TargetCurrencies.Add('USD');
// ECB API URL for XML exchange rates
ECBApiUrl := 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
// Make HTTP GET request to XML API
if gHttpClient.Get(ECBApiUrl, HttpResponseMessage) then begin
// Read the response as text
HttpResponseMessage.Content.ReadAs(ResponseText);
// Parse the response text into XML document
// Parse the response text into XML document
IsParsed := XmlDocument.ReadFrom(ResponseText, gXMLDocument);
if IsParsed then begin
// Initialize XmlNodeList before using it
XmlElement := gXmlNode.AsXmlElement();
// Try to cast gXmlNode to an XmlElement
XmlElement := gXmlNode.AsXmlElement();
if gXmlDocument.SelectNodes('/gesmes:Envelope/xmlns:Cube/xmlns:Cube/xmlns:Cube', XmlNodeList) then begin
// Iterate over each node to extract currency rates
foreach gXmlNode in XmlNodeList do begin
// GPT comment: gXmlNode is now initialized by the foreach loop
// Initialize XmlElement from gXmlNode
XmlElement := gXmlNode.AsXmlElement(); // GPT comment: Initialize XmlElement from gXmlNode
// Access XML attributes directly
xmlAttrCol := XmlElement.Attributes(); // GPT comment: Initialize xmlAttrCol with attributes from XmlElement
I := 0;
// Iterate over attributes to find currency and rate
for i := 1 to xmlAttrCol.Count do begin
if xmlAttrCol.Get(i, xmlAttr) then begin
// GPT comment: xmlAttr is initialized from xmlAttrCol.Get(i, xmlAttr)
// Check for 'currency' attribute
if xmlAttr.Name = 'currency' then
TargetCurrency := xmlAttr.Value;
if xmlAttr.Name = 'currency' then
TargetCurrency := xmlAttr.Value;
// Check for 'rate' attribute
if xmlAttr.Name = 'rate' then
if Evaluate(Rate, xmlAttr.Value) then begin
// Check if the target currency is one of our desired target currencies
if TargetCurrencies.Contains(TargetCurrency) then begin
// Insert or update the exchange rate in Currency Exchange Rate table
if CurrencyExchangeRate.Get('EUR', TargetCurrency, Today) then begin
CurrencyExchangeRate.Validate("Exchange Rate Amount", Rate);
CurrencyExchangeRate.Modify();
end else begin
CurrencyExchangeRate.Init();
CurrencyExchangeRate.Validate("Currency Code", TargetCurrency);
CurrencyExchangeRate.Validate("Starting Date", Today);
CurrencyExchangeRate.Validate("Exchange Rate Amount", Rate);
CurrencyExchangeRate.Insert();
end;
Message('I m inside 2 cu');
end;
end else
Error('Failed to convert rate for %1', TargetCurrency);
end;
end;
XmlElement := gXmlNode.AsXmlElement(); // GPT comment: Initialize XmlElement from gXmlNode
end;
end else
Error('Invalid XML structure for exchange rates.');
end else
Error('Failed to parse XML response.');
end else
Error('Unable to retrieve data from the exchange rate API. Status Code: %1', HttpResponseMessage.HttpStatusCode());
end;
var
XMLBuffer: Record "XML Buffer";
}
André Arnaud de Cal...
292,516
Super User 2025 Season 1
Martin Dráb
231,430
Most Valuable Professional
nmaenpaa
101,156