Tuesday, May 3, 2016

Setting the state of a sales order to Completed

Normally when we want to update the state we use the following lines of code to do which is more generic.

SetStateRequest setStateRequest = new SetStateRequest();
setStateRequest.EntityMoniker = new EntityReference(entityName, id);
setStateRequest.State = new OptionSetValue(State);
setStateRequest.Status = new OptionSetValue(Status);
db.Execute(setStateRequest);

Though this is generic this cannot be used with Sales Order as it has another method which has been given OOTB for this. If we use the above for it we would get an error. SO if you want to update the state to complete, please use the following lines of code.

var request = new FulfillSalesOrderRequest
            {
                OrderClose = new OrderClose()
                {
                    SalesOrderId = new EntityReference(SalesOrder.EntityLogicalName, id)

                },
                Status = new OptionSetValue(Status)
            };


db.Execute(request);

As mentioned before, you can use the first set of code to update the state and status of the other entities.

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...