Working with Plug-ins for entities with N:N relationship in DataVerse/Dynamics 365
We all know that (many-to-many) N:N relationship creates an Intersect table. When we add related record for an entity then the Event that is triggered is Associate. For N:N we cannot handle using Create or Update Message. This is specific to N:N relationship.
So Here I am presenting how we can handle the plugin code for a business scenario:
Business Scenario:
Lets say we have two entities eDoctors and eMedicines. The eDoctor entity having custom fields Name and Department. Department is a choice data type where I have added two items Urology & Diabetes. The next entity eMedicines has two custom fields Name and Category. Then category is a choice field and pointing to same choice field called as Department. My requirement is When user will try to add eMedicines for a eDoctor it should not add if the department of the doctor and the category of medicine is not same.
Solution
Now this two tables are having a N:N relationship. users can add related eMedicines for eDoctors and also users can add related eDoctors for eMedicines. Like below screenshots.
Here is eDoctors table data.
Here is the eMedicines data.
Now as we have many to many relationship between this two table so I can open an eDoctor record and click Related option then choose eMedicines and click Add Existing eMedicines.
When we select Add Existing eMedicines it will display all eMedicines and I can choose any item and select. As there is no restriction I can add any eMedicines with eDoctors.
Now as per our requirement we have to restrict the eMedicines addition to eDoctors if the department of Doctor and Category of medicine is not same.
So to do this I have written a plugin with below code:
[gist]8a88cc296fc2a3560771f9dae7b31b8e[/gist]
registered the plugin on Associate message and the primary entity and secondary entity should be none. As for Associate message we cannot select any entity rather the plugin will execute for all associations. But we need to filter our logic by the relationship name.
For me the relationship name is “cr8d1_eMedicines_cr8d1_eDoctor_cr8d1_eDoc.Referencing”. In the code the plugin will stop executing if the relationship name does not match with my relationship. To find the name of the relationship you have to go to relation ships area in DataVerse for any of the two tables you are working with and open the relationship then it will give the name. If your relationship name is ” cr8d1_eMedicines_cr8d1_eDoctor_cr8d1_eDoc” then just append “.Referencing” in the name finally you use the name in plugin.
The below condition is used to restrict and throw exception if the category of medicines and department of doctor does not match.
if (Department== 346630001 && category != 346630001)
{
throw new InvalidPluginExecutionException("You cannot add non-diabetic medicines for this doctor.");
}
else if(Department == 346630000 && category != 346630000)
{
//if department is urology and medicine category is not urology
throw new InvalidPluginExecutionException("You cannot add non-urology medicines for this doctor.");
}
After implementing this if you test and try to add non matching category and department then it will throw error.
Hope this helps.