
Hi Everyone,
Need urgent help.
I am sending data to Http request and saving the response into D365.
Below is my code,
public static void main(Args _args)
{
System.Exception ex;
System.Net.WebHeaderCollection httpHeader;
System.Net.WebClient webClient;
System.Text.UTF8Encoding encoder;
Map map;
mapEnumerator mapEnumerator;
str workorderNumberParm;
str jsonResponse;
str json = "{\"EquipmentNumber\":\"11030143\",\"ExecutingDepartmentCode\":\"2010\",\"RequestedDepartmentCode\":\"2010\",\"ScheduledDate\":\"2020-04-08\",\"PlanDate\":\"2020-04-10\",\"PlanCompletionDate\":\"2020-04-15\",\"ReasonCode\":\"RS001\",\"ReasonDescription\":\"Reason Description\",\"Description\":\"Api wokorder new Parameter\",\"Priority\":\"Normal\",\"authToken\":\"CreatingfromoutsideKey123\"}";
try
{
webClient = new System.Net.WebClient();
httpHeader = new System.Net.WebHeaderCollection();
httpHeader = webClient.Headers;
httpHeader.Add("Authorization", "Bearer CreateKey123");
httpHeader.Add("Content-Type", "application/json");
encoder = new System.Text.UTF8Encoding();
System.Byte[] encodedBytes = encoder.GetBytes(json);
System.Byte[] response = webClient.UploadData("http://217.182.200.75:8018/api/Createworkorder/Create", encodedBytes);
jsonResponse = webClient.Encoding.GetString(response);
Info(strFmt("Response : %1",jsonResponse ));
map = RetailCommonWebAPI::getMapFromJsonString(jsonResponse);
workorderNumberParm = map.lookup("WorkorderNumber");
Info(strFmt("workorderNumberParm %1", workorderNumberParm));
}
catch(Exception::CLRError)
{
ex = ClrInterop::getLastException();
if (ex != null)
{
ex = ex.get_InnerException();
if (ex != null)
{
error(ex.ToString());
}
}
}
}
In response infolog shows the desired response , the same which I got through Postman tool
which looks like
"{\"WorkorderNumber\":\"ELE/2020/00038\",\"Status\":\"Success!\",\"ErrorCode\":\"6000\"}"
However when I debug the code, the compiler shows the value of response as,
"\"{\\\"WorkorderNumber\\\":\\\"ELE/2020/00038\\\",\\\"Status\\\":\\\"Success!\\\",\\\"ErrorCode\\\":\\\"6000\\\"}\""
Due to this
map = RetailCommonWebAPI::getMapFromJsonString(jsonResponse);
I get null value in map variable.
Please suggest some solution.
Thank you in advance.
Hi Rhushikesh,
Try to use contract class and parse json to it.
community.dynamics.com/.../how-to-deserialize-json-file-in-d365fo