LWC

When would you need a one-to-many to many-to-many relationship in Salesforce

Let's try to answer this question.

How would you determine what type of relationship you need for a specific business problem?

Let’s take an example of an application with maintains the data regarding a course and Professor who teaches that course

A professor can teach many courses

A course can be taught by 1 professor. Your case may require or need multiple people to teach a course. But for the sake of this example let's assume it only going to be 1

In this case, this would look like a one-to-many relationship would be an excellent choice. And it would definately be correct.

Historical data

It will answer the questions : who is the current Professor of the course?

However, if you want to capture and store historical data. A many-to-many relationship is the way to go

You can have the table setup like so
Basically you can say physics is taught by ProfID2 But in 2020 it was taught by ProfID1.
Do you see how that can be useful?

So, next time if you are wondering which relationship to choose. Ask yourself this question. “Is there any need to maintain historical data for this relationship?”. If you answer yes to this question many-to-many would be the right option.

Leave a Reply

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