Associated views have always been a sore subject in CRM. When CRM 3 was released, they did not include inactive records and, despite the ever present Activate menu item, there was no any way to actually show all associated records not just active ones. People complained. Then rollup 2 changed the behaviour. People complained. Then there was a hotfix for custom entities. People complained that it should be available for system entities as well. CRM 4 is released and hotfix no longer works. People complain.

Plug-ins to the rescue

The good news is that CRM 4 plug-in model is flexible enough to solve this problem once and for all. The idea is to intercept RetrieveMultiple message and modify its query so that all associated records are returned. And the code is surprisingly simple:

using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;

namespace Acme.Plugins
{
    public class AssociatedViewPlugin : IPlugin
    {
        public void Execute(IPluginExecutionContext context)
        {
            if (context.InputParameters.Contains(ParameterName.Query))
            {
                QueryExpression qe = context.InputParameters[ParameterName.Query] as QueryExpression;
                // If it's RetrieveMultiple multiple with two conditions
                if (qe.EntityName == context.PrimaryEntityName
                      && qe.Criteria != null
                      && qe.Criteria.Conditions != null
                      && qe.Criteria.Conditions.Count == 2)
                {
                    // If first condition is a statecode filter for inactive entities
                    ConditionExpression ce = qe.Criteria.Conditions[0] as ConditionExpression;
                    if (ce != null
                        && ce.AttributeName == "statecode"
                        && ce.Operator == ConditionOperator.Equal
                        && ((int)ce.Values[0]) == 0)
                    {
                        // Remove statecode filter
                        qe.Criteria.Conditions.Remove(ce);
                    }
                }
            }
        }
    }
}

Installation

  1. Compile the code and sign the assembly.
  2. Using Plug-in Registration Tool register the plug-in.
  3. Register execution step for the entity that you'd like to show all records in associated views. Note that we use PreStage (a.k.a. BeforeMainOperationOutsideTransaction) and Synchronous execution mode. For example, to always show inactive contacts in associated view:
    image
  4. Optionally add Status column to the associated view for the entity not to confuse users.
  5. Rinse and repeat 3-4 for other entities.

The result for the Contacts associated view inside account entity:

image

At long last Activate menu command makes sense.

Note: this code is a sample only and is included "AS IS" without any warranties whatsoever. In Real Life™ more stringent tests would have to be performed to ensure that RetrieveMultiple message indeed originated from the associated view and was not sent by your poor fellow co-worker complaining (or, worse still, raising support request with Microsoft) about filtering state code not working.

Currently rated 3.7 by 9 people

  • Currently 3.666667/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

March 10. 2008 11:25

Trent

Just implemented this plugin and it works perfectly!

Thanks George!

Trent

January 18. 2009 21:29

pingback

Pingback from hilpers.com

Historie benutzerdefinierte Entität | hilpers

hilpers.com

April 8. 2009 19:02

pingback

Pingback from hilpers-esp.com

Desactivados en vista asociada | hilpers

hilpers-esp.com

June 17. 2009 19:52

pingback

Pingback from sberetta.it

Stefano Beretta Microsoft Dynamics CRM Blog - VISTE ASSOCIATE

sberetta.it

September 18. 2009 05:40

pingback

Pingback from lukedary.com

Customization of Associated Views in MS CRM | LukeDary.com

lukedary.com

Comments are closed