Implementing “Does not contain data” for N – N relationships through RetrieveMultiple plugin in Dynamics 365


Recently we had a requirement to create a marketing list with members (contact) which are not part of any other marketing list through Advanced Find.

In version 9.0 we have “Does not contain data” filter added in Advanced Find. However, it works only in case of 1 – N relationship.

https://nishantrana.me/2017/10/19/does-not-contain-data-filter-in-advanced-find-in-dynamics-365-july-2017-update/

Here in our case, it is an N-N relationship between Contact and Marketing List.

The solution we implemented was following –

We asked the user to select the following condition in their Advanced Find query.

Which will have the following fetch XML à

And in our Retrieve Multiple Plugin, we added a new filter condition and replaced link entity condition as shown below for implementing the outer join.

Below is the sample code for the plugin that achieves this. The plugin is Registered on Pre Retrieve Multiple of Contact.

The code –


public class PreRetrieveMultipleML : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));

// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Query") && context.InputParameters["Query"] is QueryExpression && context.MessageName == "RetrieveMultiple")
{
var query = (QueryExpression)context.InputParameters["Query"];

tracingService.Trace("Inside Plugin");

if (query != null && query.EntityName == "contact")
{
foreach (var linkedEntity in query.LinkEntities.Where(
a =>
a.LinkFromAttributeName.Equals("contactid") &&
a.LinkToAttributeName.Equals("entityid") && a.LinkToEntityName.Equals("listmember") &&
a.LinkEntities.Count == 1 && a.LinkEntities[0].LinkCriteria != null))
{
if (linkedEntity.LinkEntities[0].LinkCriteria.Conditions.Count == 1
&& linkedEntity.LinkEntities[0].LinkCriteria.Conditions[0].AttributeName.Equals("listid")
&& linkedEntity.LinkEntities[0].LinkCriteria.Conditions[0].Operator == ConditionOperator.Null)
{
// remove the existing link entity
query.LinkEntities.Remove(linkedEntity);

// create a new link entity
LinkEntity doesNotContainLinkEntity = new LinkEntity()
{
EntityAlias = "ml",
LinkFromEntityName = "contact",
LinkToEntityName = "listmember",
LinkFromAttributeName = "contactid",
LinkToAttributeName = "entityid",
JoinOperator = JoinOperator.LeftOuter
};

// add the newly created link entity
query.LinkEntities.Add(doesNotContainLinkEntity);

// create a new condition expression
var doesNotContainCondition = new ConditionExpression();
doesNotContainCondition.AttributeName = "listid";
doesNotContainCondition.Operator = ConditionOperator.Null;
doesNotContainCondition.EntityName = "ml";

//create a new filter condition
var doesNotContainFilterExpression = new FilterExpression();
doesNotContainFilterExpression.AddCondition(doesNotContainCondition);

// add this new filter in query
query.Criteria.AddFilter(doesNotContainFilterExpression);

break;
}
}
}
}
}
}

We can update the plugin’s code to be used for any other N-N relationship similarly.

Hope it helps..

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

Please share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Welcome to the Blog & Website of Paul Andrew

Technical Leadership Centred Around the Microsoft Data Platform

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Dynamics 365 Ninja!

CRM for You and Me

Customer Relationship Mgt App for the Professional

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM

EVOLVED365

Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

Sara Lagerquist

No Code Customization Concepts

innovativeaj

developer, designer, writer, fun loving, patriotic, humble and a sweet person inside out :) love and respect people who have a great sense of humor.

Temmy Wahyu Raharjo

dreaming to be a clean coder and TDD minded programmer.

%d bloggers like this: