Comment unsuccessful. Please correct the errors below.

Displaying inactive records in associated view

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.

Posted by: George Doubinski
Last revised: 05 Dec, 2012 05:25 PM

Comments

June Brehm
June Brehm
09 Jun, 2014 05:18 AM

This information is very useful. Especially for us students. buy twitter followers

10 Mar, 2008 11:25 AM

Just implemented this plugin and it works perfectly!

Thanks George!

Your Comments

Comment unsuccessful. Please correct the errors below.
Used for your gravatar. Not required. Will not be public.
Posting code? Indent it by four spaces to make it look nice. Learn more about Markdown.

Preview