Create custom N to N relationship in Dynamics CRM
As the title suggests, in this blog we are going to discuss how to create a custom N: N relationship. The topic is divided into the following parts:
1. Need of custom N: N relationship.
2. Implementing custom N: N relationship.
So, let's begin.
1. Need of custom N: N relationship
One day, I received a requirement to develop functionality where a user can add multiple users and contacts for appointments and phone calls, and later on, he can send notifications to selected contacts or users via mail. The approach was simple, to create an N: N relationship between appointment to user and contact and then add a subgrid on the form for each entity. But for activity entities, CRM does not allow you to create an N: N relationship.
Hence, a custom N: N relationship was needed to be created to achieve the required functionality.
2. Implementing custom N: N relationship
Let's take the same scenario, where I have to implement the N: N relationship between appointment to user and contact, and the same relationship needed to be done for a phone call as well. The implementation is done in such a way, that you can implement custom N: N relationships for any entity (Note:- As I mentioned earlier, it is recommended to use OOB N: N relationship if available for an entity). We are going to perform the following steps, to implement the relationship:
First Create an Organization owned entity called Custom Relationship.
Create a field called 'Primary Entity' as OptionSet. Add option set values as 'Appointment' and 'Phone Call'(you can add entities that you want)
Create a lookup field for each entity option added in the 'Primary Entity' field. In my scenario, I have created for 'Appointment' and 'Phone call'.
So now we have created quick view forms for each N: N relationship grid.
Inside Security roles, Assign 'Read', 'Append' and 'AppendTo' permission for the custom relationship entity.
Publish Custom Relationship entity.
Unsecured Configuration Description:
- lookupfield_LogicalName: It is the lookup field name of the appointment entity in the Custom relationship entity.
- lookupfield_primaryEntityValue: It is the Primary entity optionset field value for appointment from the Custom relationship entity.
To retrieve all the users and contacts attached to the appointment, write a script to retrieve N: N relationship records of Custom Relationship record linked to the appointment.
Additional Customization:
- Due to any reason, if a user does not have 'Write' access on the primary entity of the record, you can add the below ribbon customization to prevent users from adding/removing records in the subgrid. Click here for the ribbon JavaScript function.
- For users to not have access to Custom Relationship record, you can remove navigation for Custom Relationship.
Comments
Post a Comment