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);
            }

        }


Monday, 2 September 2019

Add image symbol based on condition in D365 using x++


List of symbols and usage: https://mbs.microsoft.com/files/public/CS/AX/DynamicsSymbolFont_Update8.pdf

    public display container domhasCustNotes()
    {
        DocuRef             docuRef;
        CustTable           custTable;
        ImageReference      imageReference;
        container           imagecontainer;

        select firstonly RecId from custTable
            where custTable.AccountNum == this.CustAccount;

        select firstonly RecId from docuRef
            where docuRef.RefRecId == custTable.RecId
                && docuRef.USMFSalesOrderEntry == NoYes::Yes;

        if (docuRef.RecId)
        {
            imageReference  = ImageReference::constructForSymbol('RedDiamond');
        }
        if (imageReference)
        {
            imagecontainer  = imageReference.pack();
        }
       
        return imagecontainer;
    }

Standard reference
Ex : PurchReqTable form
Method PurchReqLineDS

public display container workflowIcon(PurchReqLine _line)
        {
            ImageReference imgRef;

            workflowIconObservableLink.observe();

            if (Workflow::findWorkItemsForCurrentUser(_line).elements())
            {
                imgRef = ImageReference::constructForSymbol(ImageReferenceSymbol::Workflow);
            }
            else
            {
                imgRef = ImageReference::constructForSymbol('');
            }

            return imgRef.pack();
        }

Thursday, 10 January 2019

Complete Call center sales orders using x++ code


MCRCustPaymTable        mcrCustPaymTable;
RetailMCRChannelTable   retailMCRChannelTable;
boolean                 mcrCallCenterOrder;

//Returns whether the sales order is associated with a call center or not.
mcrCallCenterOrder = salesTable.mcrIsCallCenter();
           
if (mcrCallCenterOrder == true)
{
       select firstonly retailMCRChannelTable
                    where retailMCRChannelTable.RecId   == salesTable.RetailChannelTable;

       mcrCustPaymTable.CustAccount    = salesTable.CustAccount;
       mcrCustPaymTable.CurrencyCode   = salesTable.CurrencyCode;
       mcrCustPaymTable.Channel        = salesTable.RetailChannelTable;
       mcrCustPaymTable.LineNum        = 1;
       mcrCustPaymTable.OnAccountAuthorized = NoYes::Yes;
       mcrCustPaymTable.PercentAmount  = 100;
       mcrCustPaymTable.TenderTypeId   = retailMCRChannelTable.DOMServiceEventPaymMethod;
       mcrCustPaymTable.CustPaymType   = MCRCustPaymTable::getMCRTypeFromTender(mcrCustPaymTable.TenderTypeId,
                                                                                         mcrCustPaymTable.Channel,
                                                                                         mcrCustPaymTable.CardTypeId);
       mcrCustPaymTable.RefTableId     = salesTable.TableId;
       mcrCustPaymTable.RefRecId       = salesTable.RecId;
       mcrCustPaymTable.insert();

       MCRSalesOrderTotals mcrSalesOrderTotals = new MCRSalesOrderTotals(salesTable, true);

       //complete
       MCREndOrder::endOrder(salesTable.SalesId, true, mcrSalesOrderTotals, false);
}