Completing or Cancelling Orders via Code

In CRM 2011 you can programmatically close an order and mark it as Complete or Cancelled.

If completed, you also need to firstly specify the order fulfilled date.

...
crmSalesOrder.DateFulfilled = DateTime.Now();

Context.UpdateObject(crmSalesOrder);
Context.SaveChanges();
...

var orderClose = new OrderClose()
                        {
                            SalesOrderId =
                                new EntityReference(
                                SalesOrder.EntityLogicalName,
                                crmSalesOrder.Id),
                        };

if (...)
{
    // Mark order as Complete
    var fulfillSalesOrderRequest = new FulfillSalesOrderRequest()
                                        {
                                            OrderClose = orderClose,
                                            Status = new OptionSetValue((int)salesorder_statuscode.Complete)
                                        };
    Context.Execute(fulfillSalesOrderRequest);
}
else
{
    // Mark order as Cancelled
    var cancelSalesOrderRequest = new CancelSalesOrderRequest()
                                        {
                                            OrderClose = orderClose,
                                            Status = new OptionSetValue((int)salesorder_statuscode.NoMoney)
                                        };
    Context.Execute(cancelSalesOrderRequest);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s