Sunday, 22 August 2021

Associate/Disassociate plugin messages in CRM

 

Associate/Disassociate plugin messages in CRM


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.
Entity Associate View

Entity Associate View

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’.
Associate Plugin Step

Associate Plugin Step

  • 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];

                }

            }




***********************************************************************************


In MS CRM Plugin Associate/Disassociate messages are used only for N-N relationship adding/removal of a associate record.

Note: With 1-N relationship you will have to use Update message with filter by relationship lookup.
For handling of deleting of records from sub-grid you will have to handle Delete message.
For example if you have grid of child records on parent record then if the grid is containing 1:n relationship child records then no need to write plugin on associate and disassociate messages for any logic. For that Update message will work.

In MS CRM Plugins, the Associate & Disassociate messages behave little bit 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"  & "Disassociate" operations, so we have to check few conditions to let the “Association”  & "Disassociate" trigger happen only between intended entities. You can use the below code template for Associate or Disassociate plugins.

using System;

using System.Collections;

using System.Text;

using Microsoft.Xrm.Sdk;

using System.Web;

using System.Xml;

using System.Diagnostics;

using Microsoft.Xrm.Sdk.Query;

using Microsoft.Xrm.Sdk.Client;

using Microsoft.Crm.Sdk.Messages;

namespace Termination

{

    public class Delegates:IPlugin

    {

        public void Execute(IServiceProvider serviceProvider)

        {

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

 

            if (context.Depth > 1)

                return;

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

 

            EntityReference targetEntity = null;

            string relationshipName = string.Empty;

            EntityReferenceCollection relatedEntities = null;

            EntityReference relatedEntity = null;

            Entity eventBooking = null;

            Guid eventEntID = Guid.Empty;

            EntityReferenceCollection eqEntites = null;

            Relationship relationshipEventContact = null;

           

            try

            {

              

                #region Associate & Disassociate

                if (context.MessageName.ToLower() == "associate" || context.MessageName.ToLower() == "disassociate")

                {

                    // Get the “Relationship” Key from context

                    if (context.InputParameters.Contains("Relationship"))

                    {

                       // Get the Relationship name for which this plugin fired

                        relationshipName = ((Relationship)context.InputParameters["Relationship"]).SchemaName;                      

                    }

 

                    // Check the "Relationship Name" with your intended one

                    if (relationshipName != "new_new_eventbooking_contact")

                    {                     

                    return;                 

                    }

 

                     // Get Entity EventBooking reference from Target Key from context

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

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

                        eventBooking = service.Retrieve("new_eventbooking", targetEntity.Id, new ColumnSet("new_eventname"));

                        if (eventBooking.Attributes.Contains("new_eventname"))

                        {                         

                            var eventEnt = (EntityReference)eventBooking["new_eventname"];

                             eventEntID = eventEnt.Id;

                        }

                       }

     

                     // Get Entity Contact 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];

                        eqEntites = new EntityReferenceCollection();

                        eqEntites.Add(new EntityReference("contact", relatedEntities[0].Id));                     

                    }

                

                    if (eqEntites != null && eventEntID != null)

                    {

                        relationshipEventContact = new Relationship("new_new_events_contacts");

                        if (context.MessageName.ToLower() == "associate")

                        {

                            service.Associate("new_event", eventEntID, relationshipEventContact, eqEntites);

                        }

                        if (context.MessageName.ToLower() == "disassociate")

                        {

                            service.Disassociate("new_event", eventEntID, relationshipEventContact, eqEntites);

                        }

                        trace.Trace("Event-Delegate Associate & Dissassociate Plugin copleted Successfully");

                    }

                    else

                    {

                        trace.Trace("Event-Delegate Associate & Dissassociate Plugin not completed Successfully");

                    }

                }

                #endregion

            }

            catch (Exception ex)

            {

                trace.Trace(string.Format("Event-Delegate Associate & Dissassociate Plugin error: {0}", new[] { ex.ToString() }));

            }

        }

    }

} 

No comments:

Post a Comment

Power Apps Drag & Drop Kanban code sample

  Introduction: The Kanban Board App Developers at a software company use the Kanban board to show their progress on development tasks and b...