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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Microsoft.Dynamics.Nav.Runtime.NavXmlNode variable not initialized. error

(1) ShareShare
ReportReport
Posted on by 72
 
Hi,
I have code developed to ECB exchange rates. Codeunit is complied without error.
 
but I am getting this error
 
Microsoft.Dynamics.Nav.Runtime.NavXmlNode variable not initialized.
 
Please help me with correction code.
The code is for ECB to XML to BC table (currency exchange rate)
 
Please help with comment on this.
 
Warm Regards
Krishna
 
I have the same question (0)
  • Suggested answer
    Gerardo Rentería García Profile Picture
    26,091 Most Valuable Professional on at
  • Suggested answer
    YUN ZHU Profile Picture
    99,974 Super User 2026 Season 1 on at
    Hi, have you tried to debug?
    This seems to be a code problem. I suggest debugging it first to find out which variable has the problem.
     
    Hope this can give you some hints.
    Thanks.
    ZHU
  • KK-20050817-0 Profile Picture
    72 on at
    Please check the code and help me with 
     
    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";
    }
    
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,024 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,145 Super User 2026 Season 1

#3
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 705 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans