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.

Convert a Sales Order to an Invoice directly using C# code

In the sales process a sales order would be converted to an invoice. Most of the fields in both entities are the same.

So it would be easy for us to create an invoice from a sales order directly by assigning each sales order field to an invoice field.  This is fine, but would be a tedious task as for some projects you wouldn’t all the fields in the sales order filled in. Is there an easy way for us to achieve this?

Yes, there is. There is an OOTB functionality which has been given, in which a sales order can be converted to an invoice with few lines of code. The code is given below.

  ConvertSalesOrderToInvoiceRequest convertOrderRequest =
                    new ConvertSalesOrderToInvoiceRequest()
                    {
                        SalesOrderId = Sales Order Id,
                        ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet(true)
                    };
                ConvertSalesOrderToInvoiceResponse convertOrderResponse = (ConvertSalesOrderToInvoiceResponse)db.Execute(convertOrderRequest);
                Invoice invoice = (Invoice)convertOrderResponse.Entity;


Adding MVC 6 Controllers to a Project

When we are working with MVC projects we tend to rely on the intellisense. When creating a controller most of the time we expect the view and the code to be generated if we are using MVC.

However, sometimes we do not get this when we are creating a controller. It only creates a controller, not the view and the code. How to sort this issue out? Can we get these options to sort them?

The answer is yes, we can. This can be achieved by adding the following packages to the project.json file

    "Microsoft.ApplicationInsights.AspNet": "1.0.0-rc1",
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    "Microsoft.Extensions.CodeGenerators.Mvc": "1.0.0-rc1-final",

    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final"

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