Search code examples
c#dynamics-crmcrmmicrosoft-dynamicsxrm

How to trigger a plugin to execute when a record it is assigned via n:n-relation


Given the entities contact and topic I have created a nen n:n-relation which basically says that each contact can be an "expert" on any number of topics and each topic can have any number of experts.

Now I want a quick way to determine whether or not a contact is an expert (has topics) in order to create a filtered view of all experts for example.

The easiest way to be able to do this would be a boolean (two-option) field on contact indicating that it has related topics (true) or not (false), which I could then use to filter views etc.

This is where I fail. I tried to set up the field as Rollup and Calculated but both of those methods don't seem to be able to solve the problem.

What would be left is to create a Plugin. But this would have to be registered on the automatically created intersect-entity which seems to be kind of impossible. I don't want to create a intersect entity myself because i'd still like to have the rich subgrid-funcionality and so on.

Is it possible to implement this kind of behaviour?


Solution

  • You need to register plugin to Associate message,primary and secondary entity as none.

    In plugin, you need to check for context.MessageName ("Associate" or "Disassociate") and context.InputParameters[“Relationship”] - it will equal to name of your N:N relationship.

    code to check conditions would be something like that

    //all usual plugin stuff here
    
    if (context.InputParameters.Contains("Relationship")) {
        relationshipName = context.InputParameters["Relationship"].ToString();
    }                                   
    
    // Check the “Relationship Name” with your intended one
    if (relationshipName != "<Your relationship name>") {
        return;
    } 
    
    if (context.MessageName == "Associate") {
        //logic when role added
    }
    if (context.MessageName == "Disassociate") {
        //logic when role removed
    }
    else {
        //not interested
    }
    

    I haven't compiled the code, but it should give you idea how to proceed.