CRM 2011 – Plugin triggering off Associate message

There aren’t many examples of how to trigger off an Associate message out in the wild and it’s a little different than in CRM 4.0.

The main difference here is that you can’t register for an Associate event against a specific entity, it’s against ALL entities so your code has to handle ensuring you are executing for the correct entity inside the message.Ā  This works both OnPrem and Online.

Below I have an example where I check the target and the relationship for their logical names and schema names:

// Get Primary Entity
EntityReference target = (EntityReference)context.InputParameters["Target"];

if (target.LogicalName == "new_parententity" || target.LogicalName == "new_tutoringattendance")
{
    Relationship relationShip = (Relationship)context.InputParameters["Relationship"];

    // Get Related Entities
    EntityReferenceCollection relatedentities = (EntityReferenceCollection)context.InputParameters["RelatedEntities"];
    foreach (EntityReference rel in relatedentities)
    {
        // Check Related Entity Logical & Schema name
        if (rel.LogicalName == "contact" && relationShip.SchemaName == "new_new_schemaname_contact")
        {
            Entity parentEntity = service.Retrieve("new_parent", target.Id, new ColumnSet(true));
            Entity relatedEntity = service.Retrieve("contact", rel.Id, new ColumnSet(true));

            // Do some work...
        }
    }
}

10 Responses to “ “CRM 2011 – Plugin triggering off Associate message”

  1. Maria says:

    Hi Daryl,

    Thanks for this code example. I’m trying to get it to work, but it seems like my plugin isn’t firing at all.

    I’ve registered a step for Associate of any entity on the parent pipeline, but I’m at a bit of a loss as to how I should write the override for “EntityNames”.

    So far it’s been:
    protected override List EntityNames()
    {
    return new List { “” };
    }

    where is the schemaname of the entity/-ies the plugin is registered for. Here it isn’t registered on any, so what should I write?

    Hope you can help!

    Maria

    • Daryl Orwin says:

      Hi Maria,

      Do you have a code example you can post or send me so I can see it in context?

      Cheers

      • Maria says:

        Hi Daryl,

        Sure šŸ™‚

        public partial class CreateContactRoles : PluginBase
        {
        protected override string PluginName()
        {
        return GetType().ToString();
        }

        protected override List EntityNames()
        {
        return new List { “contact” };
        }

        protected override void MessageSwitch(IServiceProvider serviceProvider, IPluginExecutionContext PluginExecutionContext, IOrganizationServiceFactory OrganizationServiceFactory, ITracingService TracingService)
        {
        TracingService.Trace(“{0} executing MessageSwitch”, PluginName());

        try
        {
        // Get Primary Entity
        EntityReference target = (EntityReference)PluginExecutionContext.InputParameters[“Target”];
        if (target.LogicalName == “contact” || target.LogicalName == “nc_roletype”)
        {
        Relationship relationShip = (Relationship)PluginExecutionContext.InputParameters[“Relationship”];

        // Get Related Entities
        EntityReferenceCollection relatedentities = (EntityReferenceCollection)PluginExecutionContext.InputParameters[“RelatedEntities”];
        foreach (EntityReference rel in relatedentities)
        {
        //… do something
        }

  2. Daryl Orwin says:

    Maria,

    Were you able to get your plugin working?

    I haven’t been able to think of anything else that would prevent it from triggering off of the Add Existing button.

    • Maria says:

      Unfortunately not. I ended up writing it as a console app instead, which wasn’t quite as neat, but at least got me done by deadline. Thanks for your help.

    • Maria says:

      Almost a month later, and I finally found my error.

      The override of EntityNames should be

      protected override List EntityNames()
      {
      return new List() {“none”};
      }

      Hope this can help prevent other coders getting the same grey hairs as I did šŸ˜‰

      • Jalal Hussain says:

        Hi,
        I need to display related entity based on the selection of primary entity like account,contact from dropdown and load the related entity in another dropdown.

        Thanks,
        Jalal

  3. Yaqub Ahmad says:

    Hi Daryl,

    Thanks for the code i found it very helpful.

    Regards

Leave a Reply

Your email address will not be published. Required fields are marked *