Quantcast
Channel: Dynamics 365 Customer Engagement
Viewing all articles
Browse latest Browse all 286

Associate/Disassociate plugin messages in CRM

$
0
0
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"] isEntityReference)
    {
        targetEntity = (EntityReference)pluginContext.InputParameters["Target"];
    }
    // Get Entity 2 reference from "RelatedEntities" Key from context
    if(pluginContext.InputParameters.Contains("RelatedEntities") && pluginContext.InputParameters["RelatedEntities"] isEntityReferenceCollection)
    {
        relatedEntities = pluginContext.InputParameters["RelatedEntities"] asEntityReferenceCollection;
        relatedEntity = relatedEntities[0];
    }
}

Hope this helps.

--
Happy CRM'ing
Gopinath


Viewing all articles
Browse latest Browse all 286

Trending Articles