Featured

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) 
fields 
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. 

texteIement(Test1) 
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; 
begin 
Record 
"Integration Log"): 
Boolean 
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 
Record 
"Integration Log") ReturnVaIue 
: Text 
if not YourIntegrationLog. "ML File Btw . HasVaIue Then 
exit; 
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

ABC.DRIVE

This is a technology blog, focused on Dynamics products, particularly Business Central, but not only.

Although I will start posting technical how to’s, I also aim to have it as an open blog, a blog where, if you have useful technical suggestions for the Microsoft platforms (PowerApps, Dynamics, Dataverse) we can get in touch and I can post your suggestions with credits to the writer.

  • For that get in touch on email: abcdriveblog@gmail.com

Check out the new Dynamics 365 blog

%d bloggers like this: