/// <summary> /// Creates the sales item extension to an existing asynchronous. /// </summary> /// <param name="itemKey">The item key.</param> public static async Task CreateSalesItemExtensionAsync(string itemKey) { // Validate Parameters if (string.IsNullOrEmpty(itemKey)) { ConsoleHelper.WriteErrorLine(""); ConsoleHelper.WriteErrorLine("CreateSalesItemExtensionAsync: 'itemKey' parameter is required"); ConsoleHelper.WriteErrorLine(""); return; } string itemTaxSchemaKey = await GetDefaultItemTaxSchemaAsync(); try { // Build the customer that will be created SalesItemResource newSalesItem = new SalesItemResource() { IncomeAccount = "71111", BaseEntityKey = itemKey, Unit = "KM", ItemTaxSchema = itemTaxSchemaKey }; // Create the HTTP client to perform the request using (HttpClient client = new HttpClient()) { await AuthenticationProvider.SetAccessTokenAsync(client); // Build the request string request = "salesCore/salesItems/extension"; string resourceLocation = string.Format("{0}/api/{1}/{2}/{3}", Constants.baseAppUrl, AccountKey, SubscriptionKey, request); client.SetDefaultRequestHeaders(CultureKey); // It's a POST HttpRequestMessage postSalesItemMessage = new HttpRequestMessage(HttpMethod.Post, resourceLocation); // Build the json data JsonSerializerSettings settings = new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver(), Formatting = Formatting.Indented }; string salesItemContent = JsonConvert.SerializeObject(newSalesItem, settings); postSalesItemMessage.Content = new StringContent(salesItemContent, Encoding.UTF8, "application/json"); // Log Request Console.WriteLine("Request - POST"); Console.WriteLine("{0}", resourceLocation); Console.WriteLine("Request - BODY "); Console.WriteLine("{0}", salesItemContent); // Send using (HttpResponseMessage responseContent = await client.SendAsync(postSalesItemMessage)) { // Get the response if (!responseContent.IsSuccessStatusCode) { ConsoleHelper.WriteErrorLine(string.Format("Failed. {0}", responseContent.ToString())); string errorResult = await((StreamContent)responseContent.Content).ReadAsStringAsync(); ConsoleHelper.WriteErrorLine(string.Format("Content: {0}", errorResult)); throw new Exception(string.Format("Unable to create the sales item extention for Item '{0}'.", itemKey)); } // Succeeded string result = await((StreamContent)responseContent.Content).ReadAsStringAsync(); ConsoleHelper.WriteSuccessLine(string.Format("SalesItem extension created for Item '{0}': (Id = {1})", itemKey, result)); } } } catch (Exception exception) { ConsoleHelper.WriteErrorLine("Error found!"); ConsoleHelper.WriteErrorLine(exception.Message); throw new Exception(string.Format("Error creating the sales item extension for Item '{0}'.", itemKey)); } }
/// <summary> /// Creates a SalesItem Extension asynchronous. /// </summary> /// <returns>The key of the created item.</returns> public static async Task <string> CreateItemAndSalesItemExtensionAsync() { // For Sample purpose We will Create an Item And than create the SalesItem Extension. // Usefull when you allread have the Item and only want to add the salesitem extension. // (The same occurrs for all the other extensions). string itemKey = $"NewI{DateTime.Now.ToString("yyyyMMddHHmmss")}"; string itemTaxSchemaKey = await GetDefaultItemTaxSchemaAsync(); try { // Build the item that will be created ItemResource newItem = new ItemResource() { ItemKey = itemKey, ItemType = "Item", BaseUnit = "MT", Description = itemKey }; // Create the HTTP client to perform the request using (HttpClient client = new HttpClient()) { await AuthenticationProvider.SetAccessTokenAsync(client); // Build the request string request = "businessCore/items"; string resourceLocation = string.Format("{0}/api/{1}/{2}/{3}/", Constants.baseAppUrl, AccountKey, SubscriptionKey, request); client.SetDefaultRequestHeaders(CultureKey); // It's a POST HttpRequestMessage postItemMessage = new HttpRequestMessage(HttpMethod.Post, resourceLocation); // Build the json data JsonSerializerSettings settings = new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver(), Formatting = Formatting.Indented }; string itemContent = JsonConvert.SerializeObject(newItem, settings); postItemMessage.Content = new StringContent(itemContent, Encoding.UTF8, "application/json"); // Log Request Console.WriteLine("Request - POST"); Console.WriteLine("{0}", resourceLocation); Console.WriteLine("Request - BODY "); Console.WriteLine("{0}", itemContent); // Send using (HttpResponseMessage itemResponse = await client.SendAsync(postItemMessage)) { // Get the response if (!itemResponse.IsSuccessStatusCode) { ConsoleHelper.WriteErrorLine(string.Format("Failed. {0}", itemResponse.ToString())); string errorResult = await((StreamContent)itemResponse.Content).ReadAsStringAsync(); ConsoleHelper.WriteErrorLine(string.Format("Content: {0}", errorResult)); itemKey = string.Empty; throw new Exception(string.Format("Unable to create the Item '{0}'", itemKey)); } // Succeeded string result = await((StreamContent)itemResponse.Content).ReadAsStringAsync(); ConsoleHelper.WriteSuccessLine(string.Format("Item '{0}' created: (Id = {1})", itemKey, result)); // Build the salesItems that will be created SalesItemResource newSalesItems = new SalesItemResource() { IncomeAccount = "71111", BaseEntityKey = itemKey, Unit = "KM", ItemTaxSchema = itemTaxSchemaKey }; // Build the request request = "salesCore/salesItems/extension"; resourceLocation = string.Format("{0}/api/{1}/{2}/{3}", Constants.baseAppUrl, AccountKey, SubscriptionKey, request); client.SetDefaultRequestHeaders(CultureKey); // It's a POST HttpRequestMessage postSalesItemsMessage = new HttpRequestMessage(HttpMethod.Post, resourceLocation); // Build the json data string salesItemsContent = JsonConvert.SerializeObject(newSalesItems, settings); postSalesItemsMessage.Content = new StringContent(salesItemsContent, Encoding.UTF8, "application/json"); // Log Request Console.WriteLine("Request - POST"); Console.WriteLine("{0}", resourceLocation); Console.WriteLine("Request - BODY "); Console.WriteLine("{0}", salesItemsContent); // Send using (HttpResponseMessage salesItemsResponse = await client.SendAsync(postSalesItemsMessage)) { // Get the response if (!salesItemsResponse.IsSuccessStatusCode) { ConsoleHelper.WriteErrorLine(string.Format("Failed. {0}", salesItemsResponse.ToString())); string resultError = await((StreamContent)salesItemsResponse.Content).ReadAsStringAsync(); ConsoleHelper.WriteErrorLine(string.Format("Content: {0}", resultError)); itemKey = string.Empty; throw new Exception(string.Format("Unable to create the SalesItems '{0}'.", itemKey)); } // Succeeded result = await((StreamContent)salesItemsResponse.Content).ReadAsStringAsync(); ConsoleHelper.WriteSuccessLine(string.Format("SalesItems '{0}' created: (Id = {1})", itemKey, result)); } } } } catch (Exception exception) { ConsoleHelper.WriteErrorLine("Error found!"); ConsoleHelper.WriteErrorLine(exception.Message); itemKey = string.Empty; throw new Exception(string.Format("Error creating the SalesItems")); } return(itemKey); }