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

Process SOAP Request with XMLPORT and CODEUNIT

(3) ShareShare
ReportReport
Posted on by 27
Hello, 

I am having trouble with passing the XML from SOAP Request to my Codeunit. For this, I am using XMLPort to process and insert a new Customer record, just as a test. I am trying to make the simplest scenario working. 
 
My CODEUNIT:
 codeunit 50103 TestAmir
{
    [ServiceEnabled]

    procedure saveCustVend(_custVendDC: XmlPort CustXMLPort): Text
    begin
        _custVendDC.Import();
    end;

}
The XMLPort file:
xmlport 50010 "CustXMLPort"
{
    Caption = 'Customer xml port';
    Format = Xml;
    UseRequestPage = false;
    UseDefaultNamespace = true;
    schema
    {
        textelement(_custVendDC)
        {
            XmlName = '_custVendDC';
            NamespacePrefix = 'd4p1';

            tableelement(Customer; Customer)
            {
                fieldelement(AccountNum; Customer."No.")
                {
                    XmlName = 'AccountNum';
                }
                fieldelement(Name; Customer.Name)
                {
                    XmlName = 'Name';
                }
            }
        }
    }
}
 
This is the body of the Request in Postman:
<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/TestAmir/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/TestAmir">
      <_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:Name>GLOBAL BENEFITS GROUP INSURANCE</d4p1:Name>
        <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>
 
And this is the RESPONSE I get:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <s:Fault>
            <faultcode xmlns:a="urn:microsoft-dynamics-schemas/error">a:System.NullReferenceException</faultcode>
            <faultstring xml:lang="en-US">Object reference not set to an instance of an object.</faultstring>
            <detail>
                <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Object reference not set to an instance of an object.</string>
            </detail>
        </s:Fault>
    </s:Body>
</s:Envelope>
 
Any help is greatly appreciated.
 
A.
I have the same question (0)
  • Gerardo Rentería García Profile Picture
    26,427 Most Valuable Professional on at
  • Suggested answer
    DAnny3211 Profile Picture
    11,421 Super User 2026 Season 1 on at

    Hi,

    Thanks for sharing your setup! You're on the right track using an XMLPort and Codeunit to process a SOAP request in Business Central. The error you're encountering — "Object reference not set to an instance of an object" â€” typically means that the XMLPort object wasn't properly initialized or populated before calling Import().

    Here's how you can resolve it:

    1. Use XMLPort as a Variable, Not a Parameter:

      • Instead of passing the XMLPort as a parameter, instantiate it inside the Codeunit and use ImportFromStream.

      Example:

     

    procedure saveCustVend(var InStream: InStream): Text
    var
        CustXMLPort: XmlPort "CustXMLPort";
    begin
        CustXMLPort.SetSource(InStream);
        CustXMLPort.Import();
        exit('Customer imported successfully');
    end;
     
     
    1. Update the Web Service Definition:

      • Your SOAP request should send the XML content as part of the body, and the Codeunit should accept an InStream parameter.
    2. Ensure Proper XML Structure:

      • The XML must match the structure defined in your XMLPort, including namespaces and element names.
    3. Debugging Tip:

      • Add logging or use Message() to confirm whether the XMLPort is being triggered and whether the fields are being populated.

    Let me know if you'd like help adjusting the SOAP service definition or testing the XMLPort!

    Best regards,
    Daniele

  • Suggested answer
    Sohail Ahmed Profile Picture
    11,175 Super User 2026 Season 1 on at
    Maybe this will give you a hint to resolve your issue
     
     
     

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 April 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,072 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,129 Super User 2026 Season 1

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 650 Most Valuable Professional

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans