private OrderHeadCollection FillCreateOrderData() { OrderHeadCollection collection = new OrderHeadCollection(); collection.d = new OrderHead(); // Fill Create Order Data. // Get the Order from Database. OrderHead orderdata = new OrderHead(); // Order No , CustLineref and CustItemref needs to be unique to push Order to SAP. // ** OrderNo (mandatory) // ** SalesOrg (mandatory) // ** SoldTo (mandatory) // ShipTo Address // Region - SalesOrgNumberProdCat ** orderdata.Region = "NL068"; // Order No Should be Unique ** orderdata.Orderno = "15"; // CUSTID - Sold to Number ** orderdata.Custid = "1421200"; // Ship to Number orderdata.Ardrno = ""; orderdata.Orderdate = "00000000"; orderdata.Deliverydate = "00000000"; // Should be Unique orderdata.Custlineref = "Ref 123 with error text test"; // This shoudl be testing error purpose orderdata.Goodsaddr1 = ""; orderdata.Goodsaddr2 = ""; orderdata.Goodsaddr3 = ""; orderdata.Goodsaddr4 = ""; orderdata.Autlf = false; // If the Unloading Point is valid then add below EMPst & Ablad orderdata.Empst = "UNLOADING POINT 1"; orderdata.Ablad = "UNLOADING POINT 1"; orderdata.Inco1 = ""; orderdata.Zfig = false; orderdata.Type = ""; orderdata.Id = ""; orderdata.Number = "000"; orderdata.Message = ""; orderdata.LogNo = ""; orderdata.LogMsgNo = "000000"; orderdata.MessageV1 = ""; orderdata.MessageV2 = ""; orderdata.MessageV3 = ""; orderdata.MessageV4 = ""; orderdata.ESalesdocument = ""; orderdata.EText = ""; // Fill Head2ItemNav // Loop all Order Item and add into Head2IteamsNav orderdata.Head2ItemsNav = new List <Head2ItemsNav>(); Head2ItemsNav orderItem = new Head2ItemsNav(); // Basic Fields orderItem.Lineno = "000000"; orderItem.Artno = "725222"; orderItem.Ordergty = "1"; orderItem.Qtytype = "TRP"; // CustItemref should be Unique - Residence Name orderItem.Custitemref = "TESToDATA 15"; orderItem.CondValue = "0.000000000"; orderItem.Currency = ""; orderItem.CondUnit = ""; orderItem.CondPUnt = "0"; orderdata.Head2ItemsNav.Add(orderItem); // Fill Order Note Item // Loop on Order Note to add into Head2TextNav. orderdata.Head2TextsNav = new List <Head2TextsNav>(); Head2TextsNav noteItem = new Head2TextsNav(); noteItem.Tdline = "Test Note"; noteItem.Tdformat = "/"; orderdata.Head2TextsNav.Add(noteItem); // Push Create Order to collection collection.d = orderdata; //var jsonData = JsonConvert.SerializeObject(collection, settings); return(collection); }
// This method is using HttpWebRequest to POST Json Data. private OrderHead PushOrderToSAP(OrderHeadCollection collection) { OrderHead orderHeadData = new OrderHead(); try { // Get Cookies and csrfToken if (string.IsNullOrEmpty(csrfToken)) { GetCSRFToken(); } // Using the HTTPWebRequest. var cookieContainer = new CookieContainer(); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sapCreateOrderURL); // Add Credentional, Header, Token & Cookies req.Credentials = new NetworkCredential(sapUserName, sapPassword); req.Method = "POST"; req.Headers.Add("X-CSRF-Token", csrfToken); req.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; req.Accept = "application/json"; req.ContentType = "application/json"; cookieContainer.Add(new Uri(sapCreateOrderURL), cookiestopass); req.CookieContainer = cookieContainer; // Get JSON Data from Entity var jsonData = JsonConvert.SerializeObject(collection, settings); // Enclding JSON Data UTF8Encoding encoding = new UTF8Encoding(); Byte[] bytes = encoding.GetBytes(jsonData); Stream newStream = req.GetRequestStream(); newStream.Write(bytes, 0, bytes.Length); newStream.Close(); using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) { // Get the response Content from Webresponse var rawJson = new StreamReader(response.GetResponseStream()).ReadToEnd(); if (response.StatusCode == HttpStatusCode.Created) { // Deserialize the Response JSON data to Order data. OrderHeadCollection responseCollection = JsonConvert.DeserializeObject <OrderHeadCollection>(rawJson); if (responseCollection != null) { orderHeadData = responseCollection.d; } } else if (response.StatusCode == HttpStatusCode.BadRequest) { // Log for the bad request. string errorMessage = rawJson; //Log.Info(errorMessage); } } } catch (WebException ex) { var exceptionJson = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd(); // Deserialize the Response JSON data to Error Model ErrorMessage errorMessage = JsonConvert.DeserializeObject <ErrorMessage>(exceptionJson); // Log string message = errorMessage.error.message.value; //Log.Info(errorMessage); } catch (Exception ex) { // Log the message in case any exception //Log.Info(ex.Message); } return(orderHeadData); }