Plugin “Associate” example

Couldn’t find any existing examples of using the Associate message in plugins so, having figured it out myself, I thought I’d share.

The first slightly weird thing is that you can’t register an Associate message against a particular entity. Any Associate steps will be executed every time any entity is associated with another.

So, in our code, the first thing we need to do is check we’re running against the correct relationship.

if (!context.InputParameters.Contains("<strong>Relationship</strong>")) { return; }
Relationship relationship = (Relationship)context.InputParameters["Relationship"];
if (relationship.SchemaName != "hpd_hpd_invoiceperiod_incident") { return; }

On line 3 I specify the name of the relationship I want this plugin to work for. This can be found in the Relationships area of Customizations. In my case, it’s for a relationship between Cases (incident) and a custom entity called Invoice Period (hpd_invoiceperiod).

The other 2 useful parameters we can get from InputParameters are Target and RelatedEntities.

if (!context.InputParameters.Contains("<strong>Target</strong>")) { return; }
EntityReference target = (EntityReference)context.InputParameters["Target"];
if (!context.InputParameters.Contains("<strong>RelatedEntities</strong>")) { return; }
EntityReferenceCollection related = (EntityReferenceCollection)context.InputParameters["RelatedEntities"];

Relationship is a reference to the relationship which this association is working against
Target is a reference to the primary Entity – i.e. the entity of the open CRM form
RelatedEntities is a set of references pointing to the records to associate with – i.e. the records the user just selected

N.B. In my testing RelatedEntities never seemed to contain more than one reference – the plugin just gets called once per association. I coded my plugin to handle both sitations just in case.

So once these values are available, we can add whatever logic is required to allow / deny the association. In my case, I used a fetchXML query to check that the selected records havent already been associated to a record. You can perform fetchXML queries against the join tables in CRM (in my case hpd_hpd_invoiceperiod_incident) which normally aren’t available.

To deny the operation, just throw a InvalidPluginExecutionException with a relevant error message.

Leave a comment


  1. Syai

     /  December 27, 2012

    Great Article… Very useful.. Thank you.


  2. This was really helpful, thanks for the article.

  3. We’re a group of volunteers and opening a new scheme
    in our community. Your web site offered us with valuable information to work on. You’ve done an impressive job and our entire community will be grateful to you.

  4. Its such as you read my thoughts! You appear to know a lot about this, lke you wrote the e book in it oor something.
    I feel that you could do with some p.c. to forrce the message house a bit, but otther than that, this
    iss great blog. An excellent read. I will certainly be back.

  5. Immediately after I to begin with remaining a remark I seem toward consist of clicked upon the -Inform me Even though contemporary feed-back are further- checkbox and versus previously upon each individual season a remark is further I recieve 4 e-mail with the identical similar remark.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: