Integrating NAV with a 3rd party API

Innovia Newsletter blog

I recently finished a project that involves integrating NAV with a 3rd Party API.  This integration required sending/receiving xml documents via the REST service.  I haven’t been able to find a lot of information on this, so I thought I’d blog about my own experience. 

The first step is to setup a connection to the REST service.  To do this I created a variable HttpRequest of the type Automation 'Microsoft XML, v6.0'.XMLHTTP60 then I used the following code to open the connection.

CREATE(HttpRequest,TRUE,TRUE);

HttpRequest.open('GET',RestUrl,FALSE,Acct,Pswd);

HttpRequest.setRequestHeader('Content-type','application/xml');

HttpRequest.send;

This snippet of code creates and opens the connection and sets the type to xml and sends the GET command to the REST service which returns an order template xml.

Next, I want to load the xml document that is returned and add/modify/read from it before returning it via REST to the API.

I created the following variables to do so.

XmlDoc type Automation 'Microsoft XML, v3.0'.DOMDocument60

XmlNode type Automation 'Microsoft XML, v6.0'.IXMLDOMNode

XmlAttribute type Automation 'Microsoft XML, v6.0'.IXMLDOMAttribute

XmlNodeList type Automation 'Microsoft XML, v6.0'.IXMLDOMNodeList

First you need to initiate the XmlDoc variable:

IF CREATE(XmlDoc,TRUE,TRUE) THEN BEGIN

Then we need to load the xml returned from the REST.

IF XmlDoc.load(HttpRequest.responseXML) THEN BEGIN

From here we can read/add/update the xml.

I also needed to update the attribute “Customer No.” in the Element “Order”.  To do this I used the following code:

   XmlNode := XmlDoc.selectSingleNode('Order');

   XmlAttribute := XmlNode.ownerDocument.createAttribute(‘Customer No.’);

   XmlAttribute.nodeValue := FORMAT(Rec."Customer No.");

   XmlNode.attributes.setNamedItem(XmlAttribute);

This will either update or add the ‘Customer No.’ attribute of the Element ‘Order’

The next step was to add an attribute “location_id” to the Element ‘Stop’ which an order can have multiple stops. To do this I looped through the stops and then updated the attribute.

   XmlNodeList := XmlDoc.getElementsByTagName('Stop');

  FOR i := 0 TO XmlNodeList.length - 1 DO BEGIN

       XmlNode := XmlNodeList.item(i);

       XmlAttribute := XmlNode.ownerDocument.createAttribute('location_id');

       XmlAttribute.nodeValue := FORMAT(Rec."Ship-From Code");

       XmlNode.attributes.setNamedItem(XmlAttribute);

  END;

Note: If you need to save the xml to a file you can use this command:

XmlDoc.save(FilePath);

Once I was done modifying the xml document I needed to send the xml order back to the REST service using a “PUT” command.

HttpRequest.open('PUT',PostUrl,FALSE,Acct,Pswd);

HttpRequest.setRequestHeader('Content-type','application/xml;charset=UTF-8');

HttpRequest.send(XmlDoc);

This REST service will then return xml with some information I need in order to update a field in NAV.  I needed to read the attribute “id” from the element “Order” and assign it to a text variable OrdNum to use later. The code for this is similar to what we did above.

IF CREATE(XmlDoc,TRUE,TRUE) THEN BEGIN

IF XmlDoc.load(HttpRequest.responseXML) THEN

    XmlNode := XmlDoc.selectSingleNode('Order');

    XmlAttribute := XmlNode.attributes.getNamedItem('id');

    OrdNum := XmlAttribute.text;

END;

END;  

This is some of the basics on how to integrate NAV with a 3rd Party API using xml and REST services.  I Hope this helps some of you in the future who may also need to do a similar integration.

If you would like to share any of your experiences, please drop us a line at sales@innovia.com, we would be happy to hear your thoughts.

Rick Folkner

Rick Folkner

Development Consultant

Related Posts