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
- Compile the code and sign the assembly.
- Using Plug-in Registration Tool register the plug-in.
- 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:
- Optionally add Status column to the associated view for the entity not to confuse users.
- Rinse and repeat 3-4 for other entities.
The result for the Contacts associated view inside account entity:
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 4.1 by 13 people
- Currently 4.076923/5 Stars.
- 1
- 2
- 3
- 4
- 5