Tuesday, August 30, 2011

Cancel a Service Order Programatically using C#

There are times when we need to cancel a service order using out custom code. The following code helps us achieve that.


/// Active     -  0    => New - 1, Pending - 2
/// Submitted  -  1    => In Progress - 3
/// Canceled   -  2    => No Money - 4,
/// Fulfilled  -  3    => Complete - 100001, Partial - 100002
/// Invoiced   -  4    => Invoiced - 100003

           try
           {
               CancelSalesOrderRequest setstate = new CancelSalesOrderRequest
               {
                   RequestName = "CancelSalesOrder",
                   OrderClose = new OrderClose { SalesOrderId = new EntityReference("salesorder", OrderID)},
                   Status = new OptionSetValue(status),

               };

               CancelSalesOrderResponse setstateresponse = (CancelSalesOrderResponse)Service.Execute(setstate);
           }
           catch (Exception ex)
           {
               
           }


OrderID = Should be the sales order Guid that we should be cancelling.
Above the code are the statuses that we get for Status field.

No comments:

Post a Comment

Retrieving Calendar of a Bookable Resource in Dynamics

There are occasions where we need to retrieve working days and working times of a resource in Dynamics grammatically. This is quite possible...