A.BC.Drive: Send an XML message through webservice using AL

By Robert Bulugea 

I needed to send orders in XML format over webservice calls and I soon realized that although there were some resources on how you can do that with JSON text structures, there weren’t many containing XML structures. 

Let’s have a look how I achieved this in a few simple steps below: 

In my context I am logging the XML message that I am sending into a Blob field of an integration log table. 

Step1 – I created the relevant fields here: 

Table solel "Integration Log" 
Dataclassification = CustcærContent; 
Caption — •Integration Log'; 
Robert eulugea. 3 days ago I I author (Robert Bulugea) 
1 reference 
field(le; "Entry No."; Integer) 
Au t OlnC 
field(2e; "ML File BLæ"; Blob) 
DataCIa55ification — CustomerContent; 
I reteremce 
Field (30; Code [20]) 
DataCIassification CustomerContent;

Step 2 – I also created a very simple XMLPort, feel free to create one suitable to your needs. 

XaxOccurs — Once; 
"inoccurs — Once; 
XmIName - • Testl•;

Step 3 – Now what we need is to have two functions that would: 

3.1 Stream the contents of a message into our blob field, which is exactly what the function below does 

procedure YourIntegrationLog: 
XMLExport: xm1Por•t "Export ML Test"; 
MyOutStream: Outstream; 
"Integration Log"): 
YourIntegrationLog- "XML File BLOB" TextEncoding: :UTF8) 
XMLExport . SetDocunentFiIter5(VourIntegrationLog. "Docurnt No. " ) ; 
XML Export . SetDestination(MyOutStream) ; 
XMLExport . ; 
IF XMLExport. Export() then;

3.2 Stream the contents of our blob field into a text variable, exemplified in the ReadXML2Text procedure below 

local procedure ReadXML2Text(Var YourIntegrationLog: 
Instr: Instrea•; 
Line: text; 
begi n 
"Integration Log") ReturnVaIue 
: Text 
if not YourIntegrationLog. "ML File Btw . HasVaIue Then 
File BLOB"); 
Your1ntegrationLog. "XML File BLOB" 
Read T ; 
while not Inst". EOS do begin 
Instr. ReadText(Line) ; 
ReturnValue Line;

Step4 – Sending the XML message to our destination with Http Post. 

          We then send the xml text variable to a codeunit were we are using the HTTPContent.writefrom function 

To download the source code please click here (you can make use of it as it is, just include your Launch file) 

For other useful blogs and content please visit Acumen’s insights page here

MSDYN365BC Webservice HTTP Post with XML

Published by navdriven

I design and implement solutions for Microsoft Business Central (Dynamics NAV), with a focus on EDI, CRM integrations, but not only.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: