Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Unanswered

SOAP Request and passing the XML values to codeunit

Posted on by 5
Hello!
I need your help with parsing or interpreting the XML SOAP request in my Business Central Cloud. 

This is the Codeunit I have so far:
 
codeunit 50104 "AmirCustomer"
{
    [ServiceEnabled]
    procedure saveCustVend(_custVendDC: Text): Text
    var
        RootNode: XmlNode;
        AccountNum: Text[20];
        Name: Text[100];
    begin
        // Load XML from text
        if not LoadXMLNodeFromText(_custVendDC, RootNode) then
            Error('Failed to load XML document.');
        // Find the AccountNum node with namespace
        AccountNum := FindNodeTextWithNameSpace(RootNode, 'd4p1:AccountNum', 'd4p1', 'http://schemas.datacontract.org/2004/07/Dynamics.AX.Application');
        if AccountNum = '' then
            Error('AccountNum node not found.');
        // Find the Name node with namespace
        Name := FindNodeTextWithNameSpace(RootNode, 'd4p1:Name', 'd4p1', 'http://schemas.datacontract.org/2004/07/Dynamics.AX.Application');
        if Name = '' then
            Error('Name node not found.');
        // You now have both AccountNum and Name extracted from the XML
        exit('AccountNum: ' + AccountNum + ', Name: ' + Name);
    end;
    // Helper function to load XML from text
    procedure LoadXMLNodeFromText(pXMLText: Text; var pXMLRootNode: XmlNode): Boolean
    var
        lXmlDocument: XmlDocument;
    begin
        LoadXMLDocumentFromText(pXMLText, lXmlDocument);
        pXMLRootNode := lXmlDocument.AsXmlNode;
        exit(not pXMLRootNode.AsXmlElement().IsEmpty);
    end;
    // Helper function to load XML document from text
    procedure LoadXMLDocumentFromText(pXMLText: Text; var pXMLDocument: XmlDocument)
    begin
        if pXMLText = '' then
            exit;
        XmlDocument.ReadFrom(pXMLText, pXMLDocument);
    end;
    // Procedure for finding node text with namespaces
    procedure FindNodeTextWithNameSpace(pXMLRootNode: XmlNode; pNodePath: Text; pPrefix: Text; pNamespace: Text): Text
    var
        lXmlNode: XmlNode;
        lXmlNsMgr: XmlNamespaceManager;
    begin
        if pXMLRootNode.AsXmlElement.IsEmpty then
            exit('');
        lXmlNsMgr.NameTable(pXMLRootNode.AsXmlDocument.NameTable);
        lXmlNsMgr.AddNamespace(pPrefix, pNamespace);
        if pXMLRootNode.SelectSingleNode(pNodePath, lXmlNsMgr, lXmlNode) then
            exit(lXmlNode.AsXmlElement.InnerText);
        exit('');
    end;
}



With this codeunit I get this response:
 
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <s:Fault>
            <faultcode xmlns:a="urn:microsoft-dynamics-schemas/error">a:Microsoft.Dynamics.Nav.Types.Exceptions.NavNCLXmlException</faultcode>
            <faultstring xml:lang="en-US">The following exception was encountered when processing XML data: 'Data at the root level is invalid. Line 2, position 3.' at line 2 and position 3.</faultstring>
            <detail>
                <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">The following exception was encountered when processing XML data: 'Data at the root level is invalid. Line 2, position 3.' at line 2 and position 3.</string>
            </detail>
        </s:Fault>
    </s:Body>
</s:Envelope>
 
 
 
This is my SOAP request:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/AmirCustomer/saveCustVend</Action>
    <h:CallContext xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:h="http://schemas.microsoft.com/dynamics/2013/01/datacontracts">
      <h:Company i:nil="true" />
      <h:Language i:nil="true" />
      <h:MessageId i:nil="true" />
      <h:PartitionKey i:nil="true" />
    </h:CallContext>
  </s:Header>
  <s:Body>
    <saveCustVend xmlns="urn:microsoft-dynamics-schemas/codeunit/AmirCustomer">
      <_custVendDC xmlns:d4p1="http://schemas.datacontract.org/2004/07/Dynamics.AX.Application" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <d4p1:AccountNum>32659</d4p1:AccountNum>
        <d4p1:Active>No</d4p1:Active>
        <d4p1:BusinessUnit i:nil="true" />      
        <d4p1:CostCenter i:nil="true" />
        <d4p1:Currency>USD</d4p1:Currency>
        <d4p1:CustClassificationId i:nil="true" />
        <d4p1:CustGroup>YD_KURUM</d4p1:CustGroup>
        <d4p1:Donem i:nil="true" />
        <d4p1:ElectronicAddress />
        <d4p1:HospitalCode>MED</d4p1:HospitalCode>
        <d4p1:IsCustomer>Yes</d4p1:IsCustomer>
        <d4p1:IsVendor>No</d4p1:IsVendor>
        <d4p1:Name>ACME</d4p1:Name>
        <d4p1:PaymMode>Null</d4p1:PaymMode>
        <d4p1:PaymTerm>G030</d4p1:PaymTerm>
        <d4p1:PdsCustRebateGroupId i:nil="true" />
        <d4p1:PostalAddress>
          <d4p1:PostalAddress>
            <d4p1:Country>USA</d4p1:Country>
            <d4p1:County i:nil="true" />
            <d4p1:RoleType>Invoice</d4p1:RoleType>
            <d4p1:State />
            <d4p1:Street>A Delaware, USA Company,Offices Located at 7600 Corporate Center Dr #500, Miami, FL 33126 USA,</d4p1:Street>
          </d4p1:PostalAddress>
        </d4p1:PostalAddress>  
        <d4p1:TaxGroup>YI_FTR</d4p1:TaxGroup>
        <d4p1:TaxOfficeName i:nil="true" />
       <d4p1:VATNum i:nil="true" />
       <d4p1:ValueStream i:nil="true" />
<d4p1:VendGroup>Null</d4p1:VendGroup>
      </_custVendDC>
    </saveCustVend>
  </s:Body>
</s:Envelope>
 
 
For now I would just like to get AccountNum and Name xml tag values  and display them in response... later I will add code to create a new customer with all these information... The main problem for me now is the ability to pass the request data into the codeunit...
 
Thank you so much in advance
Amir.
 
  • Khushbu Rajvi. Profile Picture
    Khushbu Rajvi. 1,519 on at
    SOAP Request and passing the XML values to codeunit
  • gdrenteria Profile Picture
    gdrenteria 8,872 Most Valuable Professional on at
    SOAP Request and passing the XML values to codeunit

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

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

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,339 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,177 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans