Understanding Intersect Entities and N:N Relationships in Dynamics 365

8 Comments

In Dynamics 365, there are 3 different types of relationships:

  • one to many (1:N)
  • many to one (N:1)
  • many to many (N:N)

When a many to many relationship is created by using the standard method of clicking “New Many to Many Relationship”, an “intersect” entity is created that allows for the relationship to exist. An out of the box example of a many to many relationship is systemuserroles, which maps System Users and Roles; a user can have many roles, and a role can belong to many users. However, creating many to many relationships this way can have limitations. The alternative method is to create a “manual” many to many relationship.

In this post, we will look at creating many to many relationships using the out of the box “native” method, which creates an intersect entity for you, versus the “manual” method of creating your own intersect entity.

Native Intersect Entity

Consider the example of students and classes. A student can have many classes, and a class can have many students. Let’s create these 2 entities, Student and Class.

Now, let’s create a new many to many relationship. In the class entity, select N:N Relationships, and New:

Select Student, and Save:

The new relationship will be created in both the Class and Student:

Next, we will modify forms to allow for adding of records. Open the Student main form:

Select Navigation. You will see Classes:

Drag classes to the navigation. Save and Publish:

Do the same with the Classes form. Save and Publish:

Now let’s create a new student record in the system. Then click on the arrow:

You will see Classes:

Click Add Existing Class and New:

Enter a new class:

Add some more classes:

Likewise, open a class, and add students:

The way this works, is Dynamics 365 creates an “intersect” entity between the Students and Classes. These intersect entities are created for both system N:N relationships (e.g. systemuserroles for System User and Roles), and custom entities such as Students and Classes.

To find an intersect entity, go to the N:N relationships of a given entity, such as the User, and find the relationship, such as Security Role:

On opening the relationship, you will see the Relationship Entity Name, in this case systemuserroles:

In our student-class example, the relationship entity is called new_new_class_new_student:

Note we can retrieve data from this entity using FetchXML, as shown below using the FetchXML Builder:

However, this method is limiting. For example, the entity is hidden, therefore you cannot add additional fields the intersect entity. To get past this limitation, we can create a manual N:N relationship as below.

Manual Intersect Entity

Consider the example where we have doctors and patients. A doctor can have many patients, and a patient can have many doctors. Let’s create these as entities in Dynamics 365.

Now instead of creating a N:N relationship like above, let’s create our own entity to manage the relationship. We will create a new entity called DoctorPatients:

Now go to N:1 relationships, and create a new relationship:

Enter Doctor as the name:

Repeat, adding Patient as the new relationship:

You should now have 2 N:1 relationships defined:

We will now add fields to the main form:

Drag across the Doctor and Patient:

Save and Publish.

Next, create some test data. Doctors:

Patients:

Now, create a new DoctorPatient record:

From here, we can create a new view to give us doctor patient records:

As this intersect entity is a “standard” entity in Dynamics 365, we can add new fields to it, for example the location the doctor and patient meet. From here, you can use this in workflows, reporting etc which depending on your use case may provide the extra functionality you need.

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

8 Responses to Understanding Intersect Entities and N:N Relationships in Dynamics 365

  1. Hi Carl,

    This is the closest I have got finding a solution. Your article is very clear, but it is not quite fitting my scenario.
    We use Webroles, and we associate these with Accounts. Many to Many relationship is adx_webrole_account. (hidden I guess).
    Whilst we are comfortable with subgrids in forms to see associations. what I crave for is a view of all our accounts, with their associated webroles

    Is this just a case of having to pupulate a manual intersect entity via some sort of download/upload?? or is there an easier way!

    • Hey Mark,

      Are you able to query the relationship entity in FetchXML Builder in XrmToolBox? You might want to write a FetchXML report to display the data. Let me know if you found a better way!

  2. Hi Carl,
    The narration of scenarios are excellent with detailed steps and screenshots. Looking forward to see this kind of concepts.

  3. Hi! Thanks for the guide. Is it possible to create eg. a new student and associate him to existing classes in one go? Or are you forced to create him first and then associate? Thanks in advance!

  4. Hi,
    Is there any way to call webhook on an update of the Relationship entity?
    For eg, if you are adding records of doctors and inside that, you have created one view to add patients for doctors.
    I have created a webhook to call on the doctor entity. Still, I also want to call the same webhook on select patient records for that entity which is considered the relationship entity.

Leave a Reply

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