Procedure to call an External API from Microsoft Dynamics NAV

Introduction:

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 ‘TIBCO Cloud Integration’ as I want to perform real-time integration between Dynamics NAV and Dynamics CRM.

Pre-Requisites:

1. Microsoft Dynamics NAV
2. External API which will be called i.e. in this scenario, TIBCO Cloud Integration Mapping.

Procedure:

  1. JSON syntax will be used for storing and exchanging data from Microsoft Dynamics NAV.
  2. 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=4.0.0.0, Culture=neutral, PublicKeyToken=***’
    • JsonTextWriter’ of datatype ‘DotNet’ and Subtype: Newtonsoft.Json.JsonTextWriter.’Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=***’
    • JsonTextReader’ of datatype ‘DotNet’ and subtype: Newtonsoft.Json.JsonTextReader.’Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=***’
  3. 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);
    
  4. I have created a function ‘StartJson’ where I am calling the ‘Initialize’ variables function and then calling the ‘WriteStartObject’ of JsonTextWriter DotNet variable.
  5. 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.

  6. Create another function ‘EndJson’ and call the ‘WriteEndObject’ as below:
          JsonTextWriter.WriteEndObject;
  7. Create a function ‘GetJson’ where the JSon content is converted ToString.
          JSon := StringBuilder.ToString;
  8. 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=2.0.0.0, 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=2.0.0.0, Culture=neutral, PublicKeyToken=***’
    • HttpWebResponse’ variable of DotNet DataType and Subtype: System.Net.WebResponse.’System, Version=2.0.0.0, 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.

  9. 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.

Conclusion:

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.


Share Story :

SEARCH BLOGS :

FOLLOW CLOUDFRONTS BLOG :


Secured By miniOrange