Advertisements
Home > CRM 2011, Plug-Ins > 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];

                }

            }

🙂

Advertisements
  1. January 12, 2015 at 5:25 PM

    Thanks for the tip. However, as far as I can tell, your expression does not work correctly:

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

    This expression does not yield the relationship name. It just returns the string ‘Microsoft.Xrm.Sdk.Relationship’. Instead, you need:

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

    (I am in CRM 2015, but the APIs seem to be virtually the same.)

  2. Roop
    August 26, 2015 at 4:31 PM

    Will this work 1: N relationship. Please confirm?

    • August 26, 2015 at 5:20 PM

      Associate\Disassociate is only for N:N relationship. For 1:N you can use Update Message, if you want to trigger a Plug-in when Primary Record changes by keeping Lookup field as ‘Filtering Attribute’

  3. Antoine
    June 9, 2016 at 7:58 PM

    When i put a plugin with a step associate or dissociate it don’t activate itself when i link two entities. have i misunderstande this event?

  4. August 26, 2016 at 12:19 PM

    Suppose i have a custom entity Registration and Individuals and I have N:N relationship created. Below is my sample code not yet used in my plugin.

    public static void AssociateIndividualsToRegistration(EntityReference individual, EntityReference registration, IOrganizationService service)
    {
    //Create EntityReferenceCollection for Individuals
    EntityReferenceCollection relatedEntities = new EntityReferenceCollection();

    //Add the related entity Individuals
    relatedEntities.Add(individual);

    //Add the Registration Individual relationship schema name
    Relationship relationship = new Relationship(“poc_registration_individual”);

    //Associate the Individual record to Registration
    service.Associate(registration.LogicalName, registration.Id, relationship, relatedEntities);

    }

    Since I have never used Associate message, am curious to what does associate plugin will do exactly
    For. eg.
    Create message – when you create a new record and save.(do something)
    Update message – when you update any fields on the form and save.(do something)
    Delete message – when you delete a record or child record from subgrid(do something)

    Similarly

    associate message – when this will be triggered? I have the individual lookup field in Registration entity. I also see that when I save the registration record with individual lookup value selected. I see this record in registration entity associated view.

    Please help me understanding the associate/disassociate plugin of CRM.

    Regards,
    Ganesh A

  5. August 26, 2016 at 6:23 PM

    Associate – Triggers when you associate ‘Registration’ and ‘Individual’ (i.e., When you go to Associated View grid of either ‘Registration’ or ‘Individual’ and click ‘Add Existing button’)
    Disassociate – Triggers when you remove either ‘Registration’ or ‘Individual’ records from Associated View grid.

  6. Hero
    October 19, 2016 at 6:28 PM

    thanks a lot Rajeev you are amazing developer

  1. No trackbacks yet.

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: