Wednesday 25 June 2014

CRM 2011/ CRM 2013 Set Default Chart–Chart Pane

I have heard that there are many requirements about setting default chart in Chart Pane.

Chart in CRM is actually stored in “SavedQueryVisualization” entity, same concept with View, is stored in SavedQuery entity.

Well, if we take a look in the database, actually, it has a field: “isdefault”, again, same concept with View, but View, we can manipulate easily through the UI, set the default view. But, Chart, we cannot do it through UI.
Okay, we go to the resolution.

1. If you are using CRM OnPremise, well, you can achieve it using Database Update syntax (unsupported, but yes, it worked).

Okay, I want the user to have Default Chart for Account.
Currently, we have 4 charts:

image

I would like to set “Accounts by Owner” as Account default chart when the user first time accessing CRM and see Account Chart in the Chart Pane.

As I mentioned above, there is a field, it indicates the default one.

image

And if we take a look, there are 4 charts that by default set as isDefault chart, and when we go to the Goal, for example, for the first time, we will always see the default Chart: Percentage Achieved

image

And it is also applying the same times for lead (Leads by Rating).

image

First of all, you have to know the savedqueryvisualization Id

select SavedQueryVisualizationId, Name, IsDefault from SavedQueryVisualization
where PrimaryEntityTypeCode = 1
and Name = 'Accounts by Owner'

Then, you can use this syntax to update the database, remember, this is modify the database

Update SavedQueryVisualization
set IsDefault = 1
where SavedQueryVisualizationId = 'A3A9EE47-5093-DE11-97D4-00155DA3B01E'

Change the SavedQueryVisualizationId based on your Chart ID.

Then, here is the result:

image

In the CRM:

User will see “Accounts by Owner” as the default chart of Account.

image

2. If you are not using CRM OnPremise, you are CRM Online User, or you are not comfortable with the Database Update, or you want to something more dynamic, ah yes, you can achieve it using plugin.

Same as the first one, I want to set the default chart for Account entity, that is now I want set “New Accounts By Month” as the default chart.

Here is the code.

//Change the variable to your Entity code and Name.
For the entity code, you can find in the:

https://www.google.com.my/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=entity%20type%20code%20crm%202013

There are plenty of example, through Tool, Database, MSDN Reference, Custom code, etc.

public class SavedQueryVisualizationPlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            #region must to have

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            // Create service with context of current user
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);


            //create tracing service
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            #endregion

            #region variables
            int intReturnTypeCode = 0;
            string strDefaultChart = string.Empty;
            EntityCollection ecVisualization = new EntityCollection();

            #endregion

            try
            {
                if (context.InputParameters.Contains("Query") && context.InputParameters["Query"] is QueryExpression)
                {
                    QueryExpression qe = (QueryExpression)context.InputParameters["Query"];

                    if (qe.EntityName == "savedqueryvisualization")
                    {
                        if (qe.Criteria != null)
                        {
                            if (qe.Criteria.Conditions != null)
                            {
                                for (int i = qe.Criteria.Conditions.Count - 1; i > -1; i--)
                                {
                                    if (qe.Criteria.Conditions[i].AttributeName == "primaryentitytypecode")
                                    {
                                        intReturnTypeCode = Convert.ToInt32(qe.Criteria.Conditions[i].Values[0]);
                                        if (intReturnTypeCode == 1) // Account, 2 = Contact, etc based on the entity type code
                                        {
                                            //Change this to your chart name
                                            strDefaultChart = "New Accounts By Month"; //

                                            if (context.OutputParameters != null)
                                            {
                                                if (context.OutputParameters["BusinessEntityCollection"] != null)
                                                {
                                                    ecVisualization = (EntityCollection)context.OutputParameters["BusinessEntityCollection"];

                                                    //change the default chart
                                                    ecVisualization = ChangeDefaultChart(strDefaultChart, ecVisualization);

                                                    //set the context OutputParameters
                                                    context.OutputParameters["BusinessEntityCollection"] = ecVisualization;
                                                    // Name of the view you want to set as Default
                                                }
                                            }
                                        }
                                        if (intReturnTypeCode == 9600) // Goal
                                        {
                                            //Change this to your chart name
                                            strDefaultChart = "Goal Progress (Money)"; //

                                            if (context.OutputParameters != null)
                                            {
                                                if (context.OutputParameters["BusinessEntityCollection"] != null)
                                                {
                                                    ecVisualization = (EntityCollection)context.OutputParameters["BusinessEntityCollection"];

                                                    //change the default chart
                                                    ecVisualization = ChangeDefaultChart(strDefaultChart, ecVisualization);

                                                    //set the context OutputParameters
                                                    context.OutputParameters["BusinessEntityCollection"] = ecVisualization;
                                                    // Name of the view you want to set as Default
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                    }
                }
            }
            catch
            {
                //to prevent unexpected error;
            }
        }

        private EntityCollection ChangeDefaultChart(string strMyDefaultChart, EntityCollection ecVisualization)
        {
            foreach (Entity entity in ecVisualization.Entities)
            {
                string strChartNameVar = (string)entity["name"];
                //play with isdefault field
                if (strChartNameVar.Equals(strMyDefaultChart))
                {
                    entity["isdefault"] = new bool();
                    entity["isdefault"] = true;
                }

                else
                {
                    entity["isdefault"] = new bool();
                    entity["isdefault"] = false;
                }

            }

            return ecVisualization;

        }
    }

Then, register your plugin:

Register Plugin

Message: RetrieveMultiple

Primary Entity: savedqueryvisualization
Event: Post-operation
Execution Mode: Synchronous

With that code, when the very first time, the user go to Account, will see the default chart same as what you did set in the plugin, no matter what.

Result

Info: The plugin will overwrite the default chart, will win against the one which is defaulted in Database.

With Plugin, you can do improvement to achieve the requirement, for example, you want dynamic setting, or you want to set not only for account, just adding if condition after the Account condition one.

if (intReturnTypeCode == 1) // Account, 2 = Contact, etc based on the entity type code
{
    //Change this to your chart name
    strDefaultChart = "New Accounts By Month"; //

    if (context.OutputParameters["BusinessEntityCollection"] != null)
    {                                           
        ecVisualization = (EntityCollection)context.OutputParameters["BusinessEntityCollection"];

        //change the default chart
        ecVisualization = ChangeDefaultChart(strDefaultChart, ecVisualization);

        //set the context OutputParameters
        context.OutputParameters["BusinessEntityCollection"] = ecVisualization;
        // Name of the view you want to set as Default
    }
}
if (intReturnTypeCode == 9600) // Goal
{
    //Change this to your chart name
    strDefaultChart = "Goal Progress (Money)"; //

    if (context.OutputParameters["BusinessEntityCollection"] != null)
    {
        ecVisualization = (EntityCollection)context.OutputParameters["BusinessEntityCollection"];

        //change the default chart
        ecVisualization = ChangeDefaultChart(strDefaultChart, ecVisualization);

        //set the context OutputParameters
        context.OutputParameters["BusinessEntityCollection"] = ecVisualization;
        // Name of the view you want to set as Default
    }
}

And here is the result:

image

*NB: The default chart will appear at the very first time user login and enter into the CRM System, when the user select another chart and then go somewhere, then go back, the default will not be set anymore, it will still remain to the last state of the chart selected by User, this is the concept of pick up where you left off.

Hope it helps!

1 comment:

  1. This is a topic that's near to my heart... Many thanks!
    Where are your contact details though?

    Also visit my webpage: Convert Video to MP3

    ReplyDelete

My Name is..