Friday, 25 October 2019

Create, Read data using OData service in D365 F&O

Hello all,

I will show you, How to create sales order from external using OData service.

Below is the snippet to just create and read the data.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ODataUtility.Microsoft.Dynamics.DataEntities;
using Microsoft.OData.Client;
using Newtonsoft.Json;

namespace ODataConsoleApplication
{
    class SalesOrderCRUD
    {
        public static void createSalesOrder(Resources context)
        {
            try
            {
                //Header
                SalesOrderHeader salesOrder = new SalesOrderHeader();
                DataServiceCollection<SalesOrderHeader> salesOrderCollection = new DataServiceCollection<SalesOrderHeader>(context);
                salesOrderCollection.Add(salesOrder);

                salesOrder.CurrencyCode = "USD";
                salesOrder.InvoiceCustomerAccountNumber = "US-001";
                salesOrder.OrderingCustomerAccountNumber = "US-001";
                salesOrder.LanguageId = "en-us";
                salesOrder.DataAreaId = "USMF";

                context.MergeOption = MergeOption.OverwriteChanges;
                context.SaveChanges(SaveChangesOptions.PostOnlySetProperties | SaveChangesOptions.BatchWithSingleChangeset); // Auto generate number sequence

                //Lines
                SalesOrderLine salesOrderLine = new SalesOrderLine();
                DataServiceCollection<SalesOrderLine> salesOrderLineCollection = new DataServiceCollection<SalesOrderLine>(context);
                salesOrderLineCollection.Add(salesOrderLine);

                salesOrderLine.SalesOrderNumber = salesOrder.SalesOrderNumber;
                salesOrderLine.ItemNumber = "1000";
                salesOrderLine.OrderedSalesQuantity = 1;
                salesOrderLine.ShippingSiteId = "2";
                salesOrderLine.ShippingWarehouseId = "24";
                salesOrderLine.DataAreaId = "USMF";

                context.SaveChanges(SaveChangesOptions.PostOnlySetProperties | SaveChangesOptions.BatchWithSingleChangeset); // Batch with Single Changeset ensure the saved changed runs in all-or-nothing mode.

                Console.WriteLine(string.Format("Sales order {0} is created.", salesOrder.SalesOrderNumber));

            }
            catch (DataServiceRequestException e)
            {
                Console.WriteLine("Error {0}", e.InnerException.Message);
            }
        }


        public static void readSalesOrder(Resources context)
        {
            string company = "USMF";

            //var salesOrderRead = context.SalesOrderHeadersV2.Where(x => x.DataAreaId == company).First();

            //To read all fields information in a record
            //Console.WriteLine(JsonConvert.SerializeObject(salesOrderRead));

            //To read field by field information in a record
            //Console.WriteLine("Sales order {0} Customer {1}", salesOrderRead.SalesOrderNumber, salesOrderRead.OrderingCustomerAccountNumber);

            //To read all records
            var salesOrderRead = context.SalesOrderHeadersV2.Where(x => x.DataAreaId == company).ToList();

            for (int i = 0; i <= salesOrderRead.Count; i++)
            {
                Console.WriteLine("Sales order {0} Customer {1}", salesOrderRead[i].SalesOrderNumber, salesOrderRead[i].OrderingCustomerAccountNumber);
            }

            Console.ReadLine();
        }
    }

}


        public static void updateSalesOrder(Resources context)
        {
            try
            {
                string salesId = "000812";
                string company = "usmf";

                // Read
                var salesOrder = context.SalesOrderLines.Where(x => x.DataAreaId == company && x.SalesOrderNumber == salesId).First();

                DataServiceCollection<SalesOrderLine> salesOrderCollection = new DataServiceCollection<SalesOrderLine>(context);
                salesOrderCollection.Add(salesOrder);

                // Update
                salesOrder.SalesPrice = 169;
                context.UpdateObject(salesOrder);
                context.SaveChanges(SaveChangesOptions.PostOnlySetProperties | SaveChangesOptions.BatchWithSingleChangeset); 

                Console.WriteLine("Updated successfully for Sales order {0} Item id {1}", salesOrder.SalesOrderNumber, salesOrder.ItemNumber);
            }
            catch (DataServiceRequestException e)
            {
                Console.WriteLine("Error {0}", e.InnerException.Message);
            }

        }


1 comment: