Tuesday, 11 October 2016

Plugin on associate disassociate of members in an N to N relation

Hi guys,

In this post I will walk through the steps to create a associate disassociate plugin in Dynamics CRM. The post here covers the steps. However, I will want t o give a more generic code as well as the steps that can be put in the register file.

The associate/ disassociate message are unlike other messages in that they are not specific to any entity but trigger globally for all entities. The below shows the steps in the CRM register file:

 <Step CustomConfiguration="" Name="AssociateUsers" Description="Post-Operation of AssociateDisassociateUsers" Id="9a550c63-b28b-e611-80d5-000d3aa06eb4" MessageName="Associate" Mode="Asynchronous" PrimaryEntityName="" Rank="1" SecureConfiguration="" Stage="PostOutsideTransaction" SupportedDeployment="ServerOnly">
              <Images />
            </Step>

            <Step CustomConfiguration="" Name="DisassociateUsers" Description="Post-Operation of AssociateDisassociateUsers" Id="9c550c63-b28b-e611-80d5-000d3aa06eb4" MessageName="Disassociate" Mode="Asynchronous" PrimaryEntityName="" Rank="1" SecureConfiguration="" Stage="PostOutsideTransaction" SupportedDeployment="ServerOnly">
              <Images />
            </Step>


Since the plugin will trigger for all associate-disassociate events, care must be taken to verify the relation that triggers the event as shown in the plugin code below:

 const string AssistantUsersRelationshipName = "contoso_systemuser_systemuser";

 if (context.MessageName == "Associate" || context.MessageName == "Disassociate")
            {
                if (context.InputParameters.Contains("Relationship"))
                {
                    var relationshipName = context.InputParameters["Relationship"].ToString();
                    var doesStartWithExpectedRelationName = relationshipName.ToLower(CultureInfo.CurrentCulture).StartsWith(AssistantUsersRelationshipName);
                    if (doesStartWithExpectedRelationName)
                    {
                        if ((context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference))
                        {
                            // contains the user to which the records are associated
                            var targetEntityRef = (EntityReference)context.InputParameters["Target"];
                            var entityLogicalNameLower = targetEntityRef.LogicalName;
                            if (string.Compare(entityLogicalNameLower, "systemuser", StringComparison.CurrentCultureIgnoreCase) == 0)
                            {
                                if (context.InputParameters.Contains("RelatedEntities") &&
                                    context.InputParameters["RelatedEntities"] is EntityReferenceCollection)
                                {

                                    // get the source user from the assistant user
                                    var relatedEntities =
                                        context.InputParameters["RelatedEntities"] as EntityReferenceCollection;
                                    if (relatedEntities.Count > 0)
                                    {
                                        var relatedEntity = relatedEntities[0];
                                        // Your logic goes here
                                    }
                                }
                            }
                        }
                    }
                }
            }


The highlighted region code snippet that checks whether the relationship name that comes as part of the input parameters is as expected. The relationship above is a N:N relation between user and user record and another check (highlighted) is present that checks the logical name of the entity reference that comes with the input parameters.

The targetEntityRef variable contains the entity which is being associated/disassociated and the relatedEntity variable contains the entity reference to the source entity to/from which the entity is being associated/ disassociated.

Hope this helps you in your plugin development journey!
Cheers!

No comments:

Post a Comment