Skip to main content

Notifications

Announcements

No record found.

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 54
 
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
 
  • KK-20050817-0 Profile Picture
    KK-20050817-0 54 on at
    Microsoft.Dynamics.Nav.Runtime.NavXmlNode variable not initialized. error
    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";
    }
    
     
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 78,032 Super User 2025 Season 1 on at
    Microsoft.Dynamics.Nav.Runtime.NavXmlNode variable not initialized. error
    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
  • Suggested answer
    gdrenteria Profile Picture
    gdrenteria 14,573 Most Valuable Professional on at
    Microsoft.Dynamics.Nav.Runtime.NavXmlNode variable not initialized. error

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,430 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans