Wednesday, 20 July 2016

Associate/Disassociate plugin messages in CRM

Hi,

Most of the times to fulfill our requirement, we will creating N:N relation ship between the entities. Sometimes, we might need to perform some business logic which relating two records. For implementing we will have to write plugins and register them on "Associate" message.
 

Here is something interesting, the Associate and Disassociate messages behave little different that other messages.

When we register a plugin on Associate or Disassociate message, we have to leave Primary and Secondary entities as 'None'. As we haven't specified any entities, the plugin triggers on all Associate/Disassociate operations. We need to check few conditions to let the Association happen only between required entities.

Here is the sample which is like template for Associate/Disassociate message.

EntityReference targetEntity = null;
string strRelationshipName = string.Empty;
EntityReferenceCollection relatedEntities = null;
EntityReference relatedEntity = null;
if (pluginContext.MessageName == "Associate")
{
    // Get the "Relationship" Key from context
    if (pluginContext.InputParameters.Contains("Relationship"))
    {
        strRelationshipName = pluginContext.InputParameters["Relationship"].ToString();
    }
    // Check the "Relationship Name" with your intended one
    if (strRelationshipName != "<YOUR RELATION NAME>")
    {
         return;
    }
    // Get Entity 1 reference from "Target" Key from context
    if (pluginContext.InputParameters.Contains("Target") && pluginContext.InputParameters["Target"] is EntityReference)
    {
        targetEntity = (EntityReference)pluginContext.InputParameters["Target"];
    }
    // Get Entity 2 reference from "RelatedEntities" Key from context
    if (pluginContext.InputParameters.Contains("RelatedEntities") && pluginContext.InputParameters["RelatedEntities"] is EntityReferenceCollection)
    {
        relatedEntities = pluginContext.InputParameters["RelatedEntities"] as EntityReferenceCollection;
        relatedEntity = relatedEntities[0];
    }
}

Hope this helps.

--
Happy CRM'ing
Gopinath

2 comments:

  1. The above code will likely have problems in later versions of CRM. The value returned from the call to 'InputParameters["Relationship"]' returns an object of type Microsoft.Xrm.Sdk.Relationship. I have found that calling "ToString()" on this object gives unpredictable results. Often the string returned has an additional "." (period) character at the end (when being called on Associate). On teh Dissassociate message, the ToString() value includes some additional odd characters. For example, my relationship name is "new_office_new_address".
    In the associate call the value returned by ToString() was:
    "new_office_new_address."
    In the disassociate call the value returned by ToString() was:
    "new_office_new_address.Referenced"

    I am testing with Dynamics 365 (8.2) on premise, on a system that was originally for Dynamics CRM 2011, so code that worked before is now broken due to this odd behavior.

    Just a friendly note to watch out for the change in behavior...

    ReplyDelete
  2. Thanks Larry, will check and update the post accordingly.

    ReplyDelete