Integration: Consuming external API in D365 FO
In this blog we'll see, how to consume external API in D365.
To start this development, first of all I had configured postman to test API. Below are the high level postman configuration:
Sample JSON:
[? ?
{
? ? "ClientCode": "I182",
? ? "ClientName": "AXXX Limited",
"CustomerKey": "2121-ddf-Test-faffbdTest090"
? },
? {
? ? "ClientCode": "U027",
? ? "ClientName": "XXXs LLC",
"CustomerKey": "2121-ddf-Test-faffbdTest090"
? ? }
]
To achieve above requirement, I have wrote below code:
class ProjectSyncInboun
{
??? public static void InboundProject()
??? {
??????? ProjectSyncInbound::jsonRead(ProjectSyncInbound::ProcessAuthorization("API_URL","Authorization_Detail",
??????????? "User_Name","APIPassword"));
??? }
?
??? public static str ProcessAuthorization(str _projectAPI, str _authorization, str _userName, str _password)
??? {
??????? str???????????????????????????? apiURL??????????? = _projectAPI;
??????? str???????????????????????????? requestJson,responseJson;
??????? System.Net.HttpWebRequest?????? request;
??????? System.Net.HttpWebResponse????? response = new System.Net.HttpWebResponse();
??????? CLRObject?????????????????????? clrObject;
??????? System.Byte[]?????????????????? bytes;
??????? System.Text.Encoding??????????? utf8;
??????? System.IO.Stream??????????????? requestStream,responseStream;
??????? System.IO.StreamReader????????? streamReader;
???????
??????? System.Net.WebHeaderCollection? httpHeader;
??????? System.Net.ServicePoint???????? servicePt;
??????? str???????????????????????????? byteStr;
??????? System.Byte[]?????????????????? byteArray;
??????? System.IO.Stream??????????????? stream;
??????? System.IO.Stream??????????????? dataStream;
??????? str???????????????????????????? returnValue;
? ??????System.String?????????????????? jsonResponse;
??????? Newtonsoft.Json.Linq.JObject??? jobject;
??????
??????? requestJson =@'{
??????????? "CustomerKey":"Customer_Key_Detail",
??????????? "ModifiedFrom":"%1",
??????????? "ModifiedTo":"%2"
??????? }';
??????
??????? requestJson = strFmt(requestJson,fromDate, today());
?
??????? httpHeader = new System.Net.WebHeaderCollection();
??????? httpHeader.Add("Authorization", strFmt("%1",_authorization));
??????? httpHeader.Add("Username", strFmt("%1",_userName));
??????? httpHeader.Add("Password",strFmt("%1",_password));
??????? httpHeader.Add("Customerkey","Customer_Key_Detail");
??????? httpHeader.Add("name","application/json");
??????? System.Net.ServicePointManager::set_Expect100Continue(false);
??????? clrObject = System.Net.WebRequest::Create(apiURL);
??????? request = clrObject;
??????? request.set_Method("POST");
??????? request.set_KeepAlive(false);
??????? request.set_Headers(httpHeader);
??????? utf8 = System.Text.Encoding::get_UTF8();
?
?? ?????bytes = utf8.GetBytes(requestJson);
??????? request.set_ContentType("application/json");
??????? request.set_ContentLength(bytes.get_Length());
?
??????? requestStream = request.GetRequestStream();
??????? requestStream.Write(bytes, 0, bytes.get_Length());
?
??????? response?????????? = request.GetResponse();
?
??????? stream???????????? = response.GetResponseStream();
??????? streamReader?????? = new System.IO.StreamReader(stream);
???????????
??????? jsonResponse?????? = streamReader.ReadToEnd().ToString();
?
??????? return jsonResponse;
??? }
?
??? /// <summary>
??? /// Parse JSON and insert into staging table
??? /// </summary>
??? /// <param name = "Json"></param>
??? public static void? jsonRead(System.String?? Json)
??? {
??????? List??????????? paramAsList;
??????? ListEnumerator? le;
??????? str???????????? jsonbreak,jsonRep;
??????? container?????? paramAsCon;
??????? System.Exception??????????????? ex;
??????? int i;
??????? boolean Success;
??????? ;
?
??????? try
??????? {
??????????? paramAsList = strSplit(Json, ",");
?
??????????? le = paramAsList.getEnumerator();
??????????? while(le.moveNext())
??????????? {
??????????????? jsonRep = strReplace(strReplace(strReplace(strReplace(strReplace(le.current(),'"',''),'{',''),'}',''),']',''),'[','');
???????????????
??????????????? paramAsCon = str2con(jsonRep,":");
?
??????????????? for (i=1;i<=conLen(paramAsCon);i++)
??????????????? {
??????????????????? if(conPeek(paramAsCon, i) == 'ClientName')
??????????????????? {
??????????????????????? projectSyncInboundStg.ClientName = (conPeek(paramAsCon, i+1));
??????????????????? }
??????????????????? else if(conPeek(paramAsCon, i) == 'ClientCode')
??????????????????? {
??????????????????????? projectSyncInboundStg.ClientCode = (conPeek(paramAsCon, i+1));
??????????????????? }
??????????????????? else if(conPeek(paramAsCon, i) == 'ExpectedEndDate')
??????????????????? {
????? ??????????????????projectSyncInboundStg.ExpectedEndDate = str2Date(subStr(conPeek(paramAsCon, i+1),1, 10),321);
??????????????????? }
??????????????????? else if(strrem(conPeek(paramAsCon, i),'"') == 'CustomerKey')
??????????????????? {
?????????????????? ?????Success = true;
??????????????????? }
??????????????? }
??????????????? if(Success)
??????????????? {
??????????????????? projectSyncInboundStg.insert();
??????????????????? Success = false;
??????????????? }
???????????????????
??????????? }
???????????
??????? }
??????? catch (Exception::CLRError)
??????? {
??????????? ex = ClrInterop::getLastException();
??????????? if (ex != null)
??????????? {
??????????????? ex = ex.get_InnerException();
??????????????? if (ex != null)
??????????????? {
??????????????????? ErrorMessage = ex.ToString();
??????????????? }
??????????? }
??????? }
??????? info("Sync completed and data inserted to D365 staging");
??? }
?
}
Dynamics 365 Finance Technical Consultant Freelancer
1 年Thanks bro
Microsoft Dynamics 365 F&O - Manager | MCP |
1 年??