Dynamics NAVProcedure to call an External API from Microsoft Dynamics NAV
In this article, I will be giving the procedure to call an External API from Microsoft Dynamics NAV. The external API which I am going to call from Microsoft Dynamics NAV is ‘Scribe Online’ as I want to perform real-time integration between Dynamics NAV and Dynamics CRM.
1. Microsoft Dynamics NAV
2. External API which will be called i.e. in this scenario, Scribe Online Mapping.
- JSON syntax will be used for storing and exchanging data from Microsoft Dynamics NAV.
- I have created the following variables:
- ‘StringBuilder’ of datatype: DotNet and Subtype: System.Text.StringBuilder.’mscorlib’
- ‘StringWriter’ of datatype: DotNet and Subtype: System.IO.StringWriter.’mscorlib’
- ‘String Reader’ of datatype: DotNet and Subtype: System.IO.StringReader.’mscorlib’
- ‘Json’ of datatype: DotNet and Subtype: System.String.’mscorlib, Version=220.127.116.11, Culture=neutral, PublicKeyToken=***’
- ‘JsonTextWriter’ of datatype ‘DotNet’ and Subtype: Newtonsoft.Json.JsonTextWriter.’Newtonsoft.Json, Version=18.104.22.168, Culture=neutral, PublicKeyToken=***’
- ‘JsonTextReader’ of datatype ‘DotNet’ and subtype: Newtonsoft.Json.JsonTextReader.’Newtonsoft.Json, Version=22.214.171.124, Culture=neutral, PublicKeyToken=***’
- I have created a function ‘Initialize’ which upon calling will initialize StringBuilder, StringWriter and JsonTextWriter variables as below:
StringBuilder := StringBuilder.StringBuilder; StringWriter := StringWriter.StringWriter(StringBuilder); JsonTextWriter := JsonTextWriter.JsonTextWriter(StringWriter);
- I have created a function ‘StartJson’ where I am calling the ‘Initialize’ variables function and then calling the ‘WriteStartObject’ of JsonTextWriter DotNet variable.
- Now create a function ‘AddToJson’ which accepts two parameters as below:
The first parameter accepts the name of the Scribe field mapping i.e. name of the External API field mapping.
The second parameter is the field value which is passed from Microsoft Dynamics NAV.
- Create another function ‘EndJson’ and call the ‘WriteEndObject’ as below:
- Create a function ‘GetJson’ where the JSon content is converted ToString.
JSon := StringBuilder.ToString;
- Next I have created a function ‘UploadJSon’ which will upload and POST the parameter values from Dynamics NAV.
The UploadJson has the following parameters:
The Subtype of DotNet variable ‘String’ is ‘System.String.’mscorlib, Version=126.96.36.199, Culture=neutral, PublicKeyToken=***’
WebServiceURL is the URL which will be called, in this case I will be passing the Scribe map URL which will be hit on a particular condition to perform real time integration.
I have created two local variables for the UploadJson function as below:
- ‘HttpWebRequest’ variable of DotNet DataType and SubType : System.Net.HttpWebRequest.’System, Version=188.8.131.52, Culture=neutral, PublicKeyToken=***’
- ‘HttpWebResponse’ variable of DotNet DataType and Subtype: System.Net.WebResponse.’System, Version=184.108.40.206, Culture=neutral, PublicKeyToken=***’
The following code is written in the UploadJson function:
The CreateWebRequest, SetRequestStream, DoWebRequest and GetResponseStream functions are as below:
The CreateWebRequest accepts two parameters from which the second parameter is the HTTP request method i.e. ‘POST’ or ‘GET’. I have used ‘POST’ as this method submits data to be processed by a specified resource.
You can set the timeout required during which the External API should be called.
- Now in my codeunit’s OnRun() function I have written the following code so that the External API URL i.e. Scribe map is hit and the real time integration between NAV and CRM takes place.
I am passing five field values from Dynamics NAV.
Note: AddToJSon(‘Car_Stage’,CarStageGlobal) where ‘Car_Stage’ is the name of the Scribe field mapping and ‘CarStageGlobal’ is the NAV field value.
I have passed the Scribe map URL to UploadJson function and the Json DotNet variable.
In this way, an External API can be called from Dynamics NAV. The External API URL i.e. Scribe map URL in this case is hit within seconds and real-time integration is performed quickly. The field values changed in Dynamics NAV are immediately reflected in Dynamics CRM.