Associate/Disassociate plugin messages in CRM 2011/2013


In CRM, the Associate or Disassociate event happens:

If you have a N:N relationship between two entities and when you try to associate or disassociate records either from Associated view or Sub grid.

In Plugins, the Associate & Disassociate messages behave little different than other messages.

When you register a plugin on Associate message, you have to leave “Primary and Secondary” entities as ‘none’.

Since we don’t provide entity names, the registered Plug-in step triggers on all “Associate” operations, so we have to check few conditions to let the “Association” trigger happen only between intended entities.
You can use the below code template for Associate or Disassociate plugins

EntityReference targetEntity = null;

string relationshipName = string.Empty;

EntityReferenceCollection relatedEntities = null;

EntityReference relatedEntity = null;

if (context.MessageName == “Associate”) {

// Get the “Relationship” Key from context

if (context.InputParameters.Contains(“Relationship”)) {

relationshipName = context.InputParameters[“Relationship”].ToString();

}

// Check the “Relationship Name” with your intended one

if (relationshipName != “{YOUR RELATION NAME}”) {

return;

}

// Get Entity 1 reference from “Target” Key from context

if (context.InputParameters.Contains(“Target”) && context.InputParameters[“Target”] is EntityReference) {

targetEntity = (EntityReference)context.InputParameters[“Target”];

}

// Get Entity 2 reference from ” RelatedEntities” Key from context

if (context.InputParameters.Contains(“RelatedEntities”) && context.InputParameters[“RelatedEntities”] is EntityReferenceCollection) {

relatedEntities = context.InputParameters[“RelatedEntities”] as EntityReferenceCollection;

relatedEntity = relatedEntities[0];

}

}

Advertisements

About Dynamics CRM Guru

We are a group of CRM Consultants and have rich experience in all Microsoft CRM versions. Currently we provide training in Microsoft CRM 2015/CRM 2013 and CRM 2011.

Posted on December 19, 2014, in Microsoft CRM 2013, Microsoft CRM 2015, Microsoft Dynamics and tagged , . Bookmark the permalink. Leave a comment.

Leave a Reply

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: