Tuesday 26 July 2016

How to Fulfill an Order by C# code in Microsoft Dynamics CRM

Hi All. Sometimes we come across certain situations where we need to Fulfill an Order by our code of Plugin or Workflow. Here I am going to explain you step by step how to Fulfill an Order via C# code in Microsoft Dynamics CRM.

Step1:

Get the GUID of the Order record which you want to fulfill. Lets say "orderGuid".

Step2:

Get the optionset value of the "Status" which fulfills the Order. In my case it is 100001(Fulfill).

Step3:

Add the reference "Microsoft.Crm.Sdk.Messages" to your code.
"using Microsoft.Crm.Sdk.Messages;"

Step4:

Add the below code:

FulfillSalesOrderRequest req = new FulfillSalesOrderRequest();
req.OrderClose = new Entity("orderclose");
req.OrderClose["salesorderid"] = new EntityReference("salesorder", orderGuid);
req.Status = new OptionSetValue(100001); //Fulfill = 100001
localContext.Trace("before executing");
FulfillSalesOrderResponse resp = (FulfillSalesOrderResponse)localContext.OrganizationService.Execute(req);
localContext.Trace("after executing");

Explaination:

In CRM, we have an entity called as "orderclose". When an order gets fulfilled, an entry is created in this entity with the reference of the fulfilled order. Here, we have achieved the same thing via c# code.