Capture Associate\Disassociate Events in Plugins

Plugins can be used to execute\perform a task in a event of changing a field of a record in an entity in CRM. Update message can be used in plugins when updating a field in an entity but how can we trigger an event when adding or removing a child record exists under a Many to Many relationship?

When Many to Many relationship exists and some task to be performed when adding records\removing records to\from parent\child entities, we can register a plugin which includes the task to be performed, using plugin registration tool to trigger the plugin at the right time.

Scenario: A system contains students and it captures mandatory subjects for each student. Meaning that one student may exist many subjects (Maths, Science etc…) and one Subject can have many Students according the relationship. Lets assume that both Student and Subject entities are custom entities with new prefix in it.

The task is that; whenever we add\remove a Subject from Student we should update a text field in Student.

Plugin : 

Plugin Structure

public class StudentMandatorySubjectsPlugin : IPlugin
public void Execute(IServiceProvider serviceProvider)
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)

// Plugin Body


Check the Association and the targeted parent child entities. Association can be happen from both ways; Subject can be added to a Student and a Student can be added to a Subject. Anyhow the relationship is same when it is stored and when it is shown in CRM.

// Get the Entity Reference as the Target
EntityReference target = (EntityReference)context.InputParameters[“Target”];

// Check the Message Name and the Target Entity
if (context.MessageName == “Associate” && (target.LogicalName == “new_student” || target.LogicalName == “new_subject”))


There can be one or more many to many relationships between the same pair of entities. For an example, between Student and Subject entities, there can be a relationship for Mandatory Subjects and for Optional Subjects. Lets assume that we are considering only the Mandatory Subjects relationship between Student and Subjects. “new_student_subject_mandatory” is the name given at the time of creating the reltionship.

// Get the ralationships in the context
Relationship relationShip = (Relationship)context.InputParameters[“Relationship”];

// Get Related Entities in the context
EntityReferenceCollection relatedentities = (EntityReferenceCollection)context.InputParameters[“RelatedEntities”];

foreach (EntityReference relatedEntity in relatedentities)
// Check Related Entity Logical & Schema Name
if (relatedEntity.LogicalName == “new_student” && relationShip.SchemaName == “new_student_subject_mandatory”)
// Student ID
// relatedEntity.Id
// Peroform the Task

relatedEntity.Id gives the Guid for the child record(student) in the relationship record.


Plugin Registration : Message Must be Associated or Disassociated and there will be no Primary Entity. Also the plugin will work only in the post stage as it creates the relationship after the execution.



Author: Indika Abayarathne

MSc in IT [University of Colombo] Solutions Architect | Consultant Technologies: Power Platform | Dynamics CE | Azure

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: