Many-to-Many Relationships

In life, many-to-many relationships are recipes for pandemics. In Dynamics CRM they aren’t quite as bad, but they can be confusing until you get used to them. This article is about the Dynamics CRM variety, but if you want to read a chilling description of the real-life kind, here’s a link to Larry Brilliant’s article, The Age of Pandemics. If you need to learn how to customize Dynamics CRM, topics like the one in this article are covered in my one-day live online training class, Customizing Dynamics CRM and the xRM Platform

Background: One-to-Many Relationships

The most frequently encountered relationships between Dynamics CRM records are “one-to-many”, often referred to in shorthand as “1:N”. On any Dynamics CRM form, these are exposed as the links you see on the left-hand side. Basically, every link you see there is a record type to which the record on the form has a 1:N relationship. For example, the following figure shows an account form with the Contacts link selected. This displays all of the contact records which are related (via the “Parent Customer” lookup field on the Contact form) to the current account record

.

Before moving on to many-to-many relationships, let’s examine 1:N’s from a different perspective. Look at the next figure. It shows the flip side of the 1:N from account to contact. That is, it shows an N:1 from contact to account:

This shows how it looks from the standpoint of the “child” record. In this example, there can be multiple contact records associated with one account record, so on the contact form the account (”Parent Customer”) is selected with a lookup, from which only one account record can be selected. If you wanted one contact to be able to have multiple parent accounts, the simple 1:N from account to contact won’t let you do that. That’s an example of where you’d need a “many-to-many”.

Many-to-Many Relationships

One-to-many (1:N) relationships are common, but there are plenty of situations that require a many-to-many (”N:N”) relationship. For example, a single contact might attend one or more events; and a single event might have multiple attendees. In my experience, N:N relationships are more common than you might at first think, and there are some subtleties about how they’re implemented in Dynamics CRM. Let’s look at a few examples.

A commonly requested customization is to make the relationship between records and “Owners” more flexible. Most records in Dynamics CRM are so-called “user owned” records, which simply means that each one is associated with a single user — the “Owner” you can see an example of in the following screenshot of an Opportunity form:

Comparing this to the previous figure, you can see there’s an N:1 relationship from Opportunity to User (referred to as “Owner” on the form), which is equivalent to saying there’s a 1:N from User to Opportunity.

In this scenario, the request might be to associate more than one user with an opportunity. For example, you might have an account manager, an executive sponsor, and a sales engineer, each with certain required tasks in the sales process. Suppose you knew for sure that those three roles were the only ones you needed. In that case, you could create two new custom 1:N relationships from User to Opportunity, and add the associated lookup fields to the opportunity form:

I only had to add two, since the “Owner” was already there, and I just changed the label on the form to “Account Manager”. This works fine, but it’s hard-wired to only three users per record. What if you don’t know how many different users might need to be associated with a record? That’s a job for a true N:N relationship. The following figure shows how this can look. I’ve created a custom N:N relationship between Opportunity and User, and I referred to it as “Stakeholder”. The screenshot shows an opportunity form open with the “Stakeholders” link selected.

This looks similar to the very first screenshot in the section on 1:N’s, but it’s different in an important respect: with an N:N relationship both sides of the relationship behave exactly the same. In this example, that means that if you open a user record you can see all the opportunities for which the user is a stakeholder:

So, not only can each opportunity be associated with lots of stakeholders, each stakeholder (a.k.a. “User”) can be associated with lots of opportunities: a true N:N relationship.

To create an N:N like the one I just illustrated, assuming you’ve got System Administrator privileges, follow these steps:

  1. Open the Opportunity form’s customization UI, by clicking Settings, Customization, Customize Entities, and then double-clicking Opportunities in the list.
  2. Click N:N Relationships, then click the New Many-to-Many Relationship button on the toolbar.
  3. Select User in the “Other Entity” section, and configure the other properties like this:

Then click Save and Close twice, then publish and test your customizations.

Just remember: the value in the “label” field is how the entity in each section will be referred to from the form for the other entity. This can be confusing, and it’s probably easiest to start by using the default “Use Plural Name” option in the Display Option drop-down.

Limitations and Subtleties of “Native” N:N Relationships

The approach I just described is often referred to as a “native” many-to-many relationship. It’s a new feature in the current version of Dynamics CRM, and can be used instead of the alternative, the so-called “manual” approach. The manual approach is not new, by the way, and simply consists of two 1:N relationships. For example, I implement N:N relationships between contacts and events by creating 1:N relationships from each of those two to a third entity, “registration”. A 1:N from contact to registration, and a 1:N from event to registration gives me an N:N between contact and event.

These native N:N relationships are easy to create, but have at least one important limitation, and some subtleties that can be confusing at first. I’ll cover those here.

  • Limitation #1: All you know is whether a relationship exists – you don’t know anything about the relationship. The easiest way to understand this is to think about the manual N:N I just described. With that approach, the third entity – often referred to as the “junction” or “intersection” entity – can contain useful information. If I want to know the status of a registration, or what the registration fee was, then the manual approach works better.
  • Limitation #2: While you can use Advanced Find to query for which records are involved in an N:N relationship, you cannot add columns from the opposite side. Basically, the problem here is that Advanced Find doesn’t really work for native N:N relationships. The way I think of it is that with Advanced Find, you start at the level of the child (or “N”) record, and work your way back up. So if I want to do a query on opportunities and return in the result set lots of information from account records, I have to start with opportunities. But if two entities are related with a native N:N relationship, there is no parent or child entity, both entities being peers, and without a 1:N relationship, Advanced Find doesn’t work!

Limitation #2 is actually more general than the Advanced Find example I illustrated with. What else in Dynamics CRM depends on 1:N relationships and makes you start at the child record and work your way back up? Correct: Workflows!

Hence the following question from a very patient TrickBag reader:

I recently created a custom entity (Network Event)that has a N:N relationship to Contact. What I’m trying to do is identify Network Events at which I meet contacts (new and existing).  So for any Network Event there may be several Contacts, and for any Contact there may be several Network Events.  I used the native N:N relationship, and it works just as I want it to.


What I am having a problem with is creating a Workflow that will update two custom fields on the Contact record to show the date of the event and the name of the event.  I’m just trying to indicate the last activity with my Contacts.  I’ve been successful in creating workflows for Tasks, E-mail, Appointments, etc. that update the two custom fields on the Contact record.


When I try to create A Workflow from the Network Event that will update the Contact record, the Contact is not an option as a Related Entity.  What am I missing?

 

Stephen, I’m sorry it took me so long to answer, and I’m sure you know this by now, but the answer is you aren’t missing anything except that intersection entity I mentioned above. Neither Advanced Find nor Workflows gives you a handle to columns from entities on the other side of a native N:N relationship. That’s why you could do it for your Task, E-Mail and Appointment activities: they all have N:1 relationships to contacts. But the way you modeled your contacts and networking events won’t work for that. Try creating a third entity, “Networking Event Attendance”, and give it fields for the date and name of the event. Then, when you write your workflow on the Networking Event Attendance entity, it will work fine.

And in Conclusion…

Well, this article certainly turned out longer than I thought it was going to be when I started it. I guess if you’ve read this far, you’re really committed, so I’ll make two more observations:

  1. Although Advanced Find doesn’t work with the native N:N approach, the Report Wizard does work. In the opportunity to user/stakeholder N:N example I used, you can create a report with Opportunities as the parent record type and “Stakeholders” as the related record type, and you can group on opportunity, displaying within each group one row for every stakeholder.
  2. Another good example of where an N:N relationship comes in handy has to do with synchronizing records to the Dynamics CRM Outlook client. Suppose you and your users want to synchronize records locally (Local Data Groups, in the Outlook client), and you can’t come up with any convenient criteria to use. You might put a checkbox on a form that’s a Yes/No to synchronize…but the problem with that is if it’s selected and used to synchronize…it synchs for everybody! A good solution here is to create a native N:N between the entity in question and the User entity. Call it “Synching Users” or something like that, and let users add themselves as a “synching user” for any record they need to synch. It works great.

If you read this far I’m eternally grateful, not to mention impressed with your dedication. Thank you very much, and good luck with all your relationships, Dynamics CRM and otherwise.

P.S., if you liked this article, you will positively adore my upcoming Dynamics CRM Essentials sessions, especially the ones on Customization. I hope to see you in an upcoming session!

14 Comments »

  1. Q&A: Dashboards; Relationships; Waits and TimeOuts Said,

    April 2, 2010 @ 7:16 am

    [...] Question: How do I create one-to-many and many-to-many relationships in CRM, and how do I know which one is appropriate for my requirements? Answer: This is one of the most frequent questions I get in the “customizing” area, and a solid understanding of how to build entity relationships is one of the most important things customizers need to know! You can customize the behavior of relationships between “system entities” (e.g., accounts and contacts), and you can create new relationships, between both system entities and custom entities. Here’s an article that contains background information on creating “one-to-many” relationships, plus step-by-step details on how to create “many-to-many” relationships in Dynamics CRM.    [...]

  2. aarch Said,

    May 11, 2010 @ 3:55 am

    Hi Richard,

    I was trying to craete N:N relationship between contacts and Leads.What i understood from your post is that once we create a N:N relationship with any of the Lead or contact entity we will be able to see all leads in Contact form and Aall Contacts in Lead form.
    But when i tested what i found was, we have to craete a n;n relationship in both Lead and Contact entity to view all leads in Contact form and Aall Contacts in Lead form.
    Am i correct??

  3. Richard Knudson Said,

    May 13, 2010 @ 8:27 pm

    Hi aarch,

    Sorry for the slow response, but I’ve been puzzling over your question. Why do you have an N:N relationship between contacts and leads? What problem are you trying to solve with that? I’m trying to think of why one lead would be related to many contacts, and one contact in turn to many different leads. Can you help me understand your scenario a little better?

    Richard

  4. aarch Said,

    November 8, 2010 @ 3:36 am

    hi richard,

    actually there may not be any business needs..but i was just trying to do the same incase any requirement for custom entities comes in a similar way .so will i need to create the N:N in both entities to get list of related records in both the entities??

  5. MP Said,

    December 2, 2010 @ 6:01 am

    Your blogs are great source of information. You have explained some of the most obscure and misunderstood CRM concepts. Please keep up the good work

    MP

  6. Andrew Stegmaier Said,

    December 29, 2010 @ 7:07 am

    This is a wonderful blog post. However, I was wondering if there have been any significant changes in the N:N relationship in crm 2011.

    I am trying to create a many to many relationship between an account and a custom “deal” entry. I would like to track things like “interest level” that are about a particular deal-account relationship. It sounds like in dynamics 4.0 i’d need to create an intermediate entity with fields such as interest level and relate this entity to both deals and accounts in two 1:N relationships. Does this advice still applies to dynamics crm 2011 or have there been improvements with the N:N relationship that would allow me to accomplish this “natively?”

  7. Richard Knudson Said,

    January 6, 2011 @ 9:00 pm

    Hi Andrew,

    As far as I can tell, there’s no significant differences between 4.0 and 2011 regarding the issue you asked about: there are two ways to create an N:N relationship:

    1. the native approach is easy and works fine for plenty of things, but there’s no intersection entity so you can’t track anything other than whether there IS a relationship or NOT.

    2. the manual approach requires 2 1:N’s, and while a little more time-consuming (less so in 2011!) it gives you more information since you have the intersection record.

  8. Monica Randwijk Said,

    March 10, 2011 @ 8:54 am

    Hi Richard,

    Because you seem to know and understand an awful lot about relationships, I’d like to ask yoiu a question about the manual approach. I have created this kind of relationship for 2 tables: human resources and the languages they speak with the extra field containing information about the level on which they speak it. But now with the advanced find view I have a problem when I want to find human resources that speak eg English on an excellent level and French on a basic level. I chose the language and the level and group them with AND, do the same with the other language, and then these two groups I group again with an AND. I think that the advanced find is looking for one record that responds to all these conditions which of course is impossible. But I don’t know how do construct it differently and get the results I want. Do you have some advice on this?

  9. Evan Said,

    April 1, 2011 @ 2:49 pm

    We are trying to make use of the relationship between two entities in CRM to accomplish the following: We have a one to many relationship between Orders and a custom entity called Circuit Design. During the completion of the Order form certain attributes are “known” and carried over to a new Circuit Design form; we will call one such field “connection a”. Connection A is known to the Provisioning group in our company and the Engineering group uses that attribute to make a new (custom entity form) called Circuit design and connect Connection A with Connection Z. The desired behavior is to have a field on the Order form called Connection Z be populated by the Circuit Design Form. I have tried almost every combination of relationships and am unable to transfer the field value from Circuit Design back to Order.

    Any help or suggestions?

  10. Richard Knudson Said,

    April 5, 2011 @ 8:31 am

    Hi Evan,

    Hmmm…help me understand your scenario better and I’ll see if I can help out. Starting with your 1:N from Order to Circuit Design, you’re saying a field on Order called Connection A gets populated to Circuit Design. Is that through a field mapping? Then, are you saying you’d like to have a field from Circuit Design (Connection Z) map back to and auto-populate a field on Order? If the relationship from order to circuit design is 1:N, how will you map fields back from circuit design to order?

    Is there a third entity somewhere playing the role of the intersection entity between the two?

  11. Ardent Fan Said,

    April 20, 2011 @ 2:52 am

    Awesome article!!! I have been seraching for this and i landed here

  12. Alex Said,

    June 7, 2011 @ 3:35 am

    Nice description of all details! Thanks.

  13. Chandan Said,

    July 19, 2011 @ 5:43 pm

    Awesome information. Thanks and keep it up !!

  14. Richard Knudson Said,

    July 19, 2011 @ 5:46 pm

    Thanks Chandan — I appreciate it!

    Richard

Leave a Comment