Search code examples
dynamics-crm-2011dynamics-crm

How do I add a CRM plugin step on a Many-to-Many Intersect Table?


I have created a (many-to-many) relationship between Opportunity and a custom entity. CRM automatically breaks the relationship with a built-in table known as an "Intersect table".

In the Plug-in Registeration Tool, I would like to add Create/Update/Delete messages on this particular table. But the problem is that it does not exist there - even many-to-many system relationships do no exist there.

People might advise me to manually break the relationship in order to be shown in the registration tool. But is there any solution to access this built-in table?


Solution

  • The answer unfortunately will be no. The paradigm in the CRM concerning many to many tables/relationships is that they are available for Association and Disassociation requests but not for Create, Update, or Delete requests. (In fact, there is no real Update on an intersection table - new associations are only either made (Create => Associate) or dissolved (Delete => Disassociate).)

    You can verify this in a way by looking at the AttributeMetadata for your intersection entity, and you'll see that all the attributes in your entity have the fields ValidForUpdateAPI and ValidForCreateAPI set to false. This differs from regular entities, which have a mix of attributes that are valid for update and create calls.

    In order to solve your problem, your options are limited:

    1) Register your step against the Association message. This message, unfortunately, can't be filtered to particular entities, so you will have to filter all association requests made in the CRM through this plugin. The IPluginExecutionContext has a property called "Relationship" in the InputParameters property bag you can use to filter out the relationship you want to develop code for.

    Relationship entityRelationship = 
        (Relationship)context.InputParameters["Relationship"];
    if (entityRelationship.SchemaName = customIntersectTable.EntityLogicalName)
    {
        EntityReference targetEntity =
            (EntityReference)context.InputParameters["Target"];
         EntityReferenceCollection relatedEntities = 
            (EntityReferenceCollection)context.InputParameters["RelatedEntities"];
        //do stuff
    }
    

    2) Make a custom intersect entity that has one to many relationships with Opportunity and your custom entity (similar to something like OpporunityProduct, which links opportunities with products). The downside here is that the GUI for this kind of thing isn't nearly as nice for this as it is for simple intersection entities.

    Gonzalo Ruiz has a blog on this topic on MSDN as well. He pretty much says the same thing.