Activity Forums Salesforce® Discussions How to create many to many relationships between two Salesforce Objects?

  • PRANAV

    Member
    March 23, 2018 at 1:44 pm

    Hi Kapil,

    When modelling a many-to-many relationship, you use a junction object to connect the two objects you want to relate to each other.

    Creating the many-to-many relationship consists of:

    • Creating the junction object.
    • Creating the two master-detail relationships.
    • Customizing the related lists on the page layouts of the two master objects.
    • Customizing reports to maximize the effectiveness of the many-to-many relationship.

    For reference search salesforce documentation "Create a Many-to-Many Relationship".

    Hope this helps you.

  • Archit

    Member
    March 26, 2018 at 8:03 am

    Hello Kapil,

    We can create many – to – Many relationship by using junction object. Junction object is a custom object with two master detail relationships.

    Thanks!

  • Parul

    Member
    September 18, 2018 at 1:37 pm

    Hi Kapil,

    You can use master-detail relationships to model many-to-many relationships between any two objects. A many-to-many relationship allows each record of one object to be linked to multiple records from another object and vice versa. For example, you create a custom object called “Bug” that relates to the standard case object such that a bug could be related to multiple cases and a case could also be related to multiple bugs. When modeling a many-to-many relationship, you use a junction object to connect the two objects you want to relate to each other.

    Junction Object
    A custom object with two master-detail relationships. Using a custom junction object, you can model a “many-to-many” relationship between two objects. For example, you create a custom object called “Bug” that relates to the standard case object such that a bug could be related to multiple cases and a case could also be related to multiple bugs.
    Creating the many-to-many relationship consists of:

    1.Creating the junction object.
    2.Creating the two master-detail relationships.
    3.Customizing the related lists on the page layouts of the two master objects.
    4.Customizing reports to maximize the effectiveness of the many-to-many relationship.

    Thanks

  • shariq

    Member
    September 20, 2018 at 11:40 pm

    Hi,

    You need to create third object which will be called junction object and relate the other two objects to through master detail or lookup

    Hope this helps.

  • Parul

    Member
    September 21, 2018 at 4:02 am

    Salesforce supports 2 kinds of relationships like Master Detail and Lookup. They are both one-to-many relationship, and they are both  defined from the many-to-one side, that is from a child to a parent. They can be made one-to-one relationship by adding validation rules, or maybe triggers to enforce the one-to-one nature, i.e. only one child is allowed.

     

    Junction objects are used to create many to many relationships between objects. If you take the Recruiting application example, you can see that a Position can be linked to many Candidates, and a Candidate can apply for different Positions. To create this data model you need a third object "Job Application" that links the 2.

     

    So you'd create a lookup field for both Position and Candidate object on the "Job Application" object. This will establish many to many relationship between Position and Candidate via the "Job Application" object known as the junction object.

     

    Fore more information, read some article

  • Aman

    Member
    September 29, 2018 at 7:09 pm

    Hi,

    Creating Many to Many relationship in salesforce is little tricky. You cannot create this type of relationship directly. Follow below steps to create this type of relationship.
    Create both objects which should be interlinked.
    Create one custom object (also called as junction object), which should have auto number as unique identification and create two master relationships for both objects, no need create tab for this object.
    Now on both objects, add this field as related list.

    Thanks

  • Avnish Yadav

    Member
    September 29, 2018 at 7:45 pm

    Hello,

    You can use Junction Object to create many to many relationships.

    thanks.

  • akash

    Member
    March 10, 2022 at 5:59 am

    I have a question for everyone,
    Why can't we create many-many relationships using lookup relationships?

  • CRMJetty

    Member
    March 25, 2022 at 10:26 am

    <style type="text/css"></style>Hello kapil,
    You can use master-detail relationships to model many-to-many relationships between any two objects. A many-to-many relationship allows each record of one object to be linked to multiple records from another object and vice versa. For example, you create a custom object called Bug that relates to the standard case object such that a bug could be related to multiple cases and a case could also be related to multiple bugs. When modeling a many-to-many relationship, you use a junction object to connect the two objects you want to relate to each other.Junction Object
    Hello kapil,You can use master-detail relationships to model many-to-many relationships between any two objects. A many-to-many relationship allows each record of one object to be linked to multiple records from another object and vice versa. For example, you create a custom object called Bug that relates to the standard case object such that a bug could be related to multiple cases and a case could also be related to multiple bugs. When modeling a many-to-many relationship, you use a junction object to connect the two objects you want to relate to each other.Junction ObjectA custom object with two master-detail relationships. Using a custom junction object, you can model a &ldquo;many-to-many&rdquo; relationship between two objects. For example, you create a custom object called &ldquo;Bug&rdquo; that relates to the standard case object such that a bug could be related to multiple cases and a case could also be related to multiple bugs.Creating the many-to-many relationship consists of:Creating the junction object.Creating the two master-detail relationships.Customizing the related lists on the page layouts of the two master objects.Customizing reports to maximize the effectiveness of the many-to-many relationship.Reference: https://help.salesforce.com/s/articleView?id=sf.relationships_manytomany.htm&amp;type=5Hope this helps.Thanks.

    • This reply was modified 2 years, 9 months ago by  CRMJetty.
    • This reply was modified 2 years, 9 months ago by  CRMJetty.
    • This reply was modified 2 years, 9 months ago by  CRMJetty.
    • This reply was modified 2 years, 9 months ago by  CRMJetty.
    • This reply was modified 2 years, 9 months ago by  CRMJetty.
  • Vipul

    Member
    October 5, 2022 at 11:14 am

    Hi All,

    Thanks for having this discussion on Junction Object. I understood from this thread that you can create Many-Many relationship between two objects. But what if i want to create Many-Many relationship between more than 2 objects.
    For example, i want to create one common custom object to build relationship between Contract, Opportunity, Orders, Job, Applications, Candidates etc..
    Why should i keep creating multiple custom junction object instead just create one common custom object with below fields like
    I understood we will not achieve many functionality supported by Master-Detail or lookup bus is there any option....
    Parent Record ID --> Polymorphic field
    Parent Object Name
    Child Record ID -> Polymorphic field
    Child Object Name
    Relationship type
    and few more fields

    Thanks
    Vipul

Log In to reply.

Popular Salesforce Blogs