Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 36 ยท Objects, Fields and Data Model

Relationships: Lookup, Master-Detail and Many-to-Many

Understand Salesforce relationships: lookup versus master-detail, hierarchical and external lookups, junction objects for many-to-many, and the effects on security and deletion.

Objects become useful when they are connected: a contact belongs to an account, an opportunity to an account, a project has tasks. Salesforce stores a link as a relationship field on the child record that points to the parent record. This lesson compares the main relationship types and shows how to model one-to-many and many-to-many structures.

Parent and child

The record holding the link is the child (the "many" side). The record it points to is the parent (the "one" side). One account (parent) has many contacts (children). The field on Contact that points to Account is a lookup field.

Lookup relationships

A lookup is a loose link.

  • The field can be optional: a child can exist without a parent.
  • Deleting the parent leaves the child; you choose the behavior when you create the field: Clear the value (default), Don't allow deletion of the lookup record that's part of a lookup relationship, or (for some cases) Delete this record also.
  • The child has its own owner and sharing, independent of the parent.
  • You cannot create roll-up summary fields from a lookup (with some exceptions via flows or add-ons).
  • A lookup can have a filter so users can pick only certain parents, for example only accounts of Type "Customer".

Master-detail relationships

A master-detail is a tight link between a master (parent) and a detail (child).

  • The master field is required on every child record.
  • Deleting the master deletes its details (cascade delete). Undeleting the master restores them.
  • The detail has no owner field of its own; it inherits the owner and sharing from the master. Access to the master controls access to the details.
  • You can create roll-up summary fields on the master that count or summarize the details.
  • An object can have up to two master-detail relationships (which is how junction objects work).
  • A standard object cannot be on the detail side. A custom object may be the detail of a standard master (such as Account) but not the reverse.
  • Reparenting the detail to another master is optional: a checkbox "Allow reparenting" decides whether users may change the master.

Converting a lookup to master-detail requires every child record to have a parent first. Converting master-detail back to lookup is blocked while roll-up summary fields exist on the master. Plan the choice before loading data.

Choosing between them

Question Lookup Master-detail
Can the child exist without a parent? Yes No
Should deleting the parent delete children? Usually not Yes
Does the child need its own owner and sharing? Yes No, inherits
Need roll-up summary fields? No Yes
Standard objects as child? Yes No
Python Stand-in: cascade delete in master-detail versus lookup

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Relationship types beyond the basics

  • Hierarchical relationship: a special lookup from the User object to itself (a manager field).
  • Self-relationship: a lookup to the same object, for example Account to Parent Account, or a Project to a "Predecessor" project.
  • External lookup and indirect lookup: link to records in an external object by external ID.
  • Many-to-many through a junction object: see below.

Junction objects (many to many)

Sometimes each side has many on the other: a student takes many courses and a course has many students. Salesforce has no direct many-to-many field. You create a junction object, a custom object with two master-detail relationships, one to each parent. Each junction record represents one pairing (an Enrollment).

erDiagram
    STUDENT ||--o{ ENROLLMENT : "master-detail"
    COURSE ||--o{ ENROLLMENT : "master-detail"
    ENROLLMENT {
        string Grade
        date EnrolledOn
    }

Enrollment is the junction object: two master-detail fields, plus its own attributes such as Grade.

Junction details to remember:

  • The first master-detail you create is the primary relationship: it decides the junction record's owner and sharing. The second is secondary. Choose the primary deliberately.
  • Deleting either master deletes its junction records.
  • Standard junction objects include Opportunity Contact Role and Campaign Member.
Python Stand-in: a junction object

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Relationship names and traversing

When you create a relationship, Salesforce asks for a Child Relationship Name (plural, for related lists, for example Enrollments) and a Field Name that becomes the relationship name Student__r. In formulas you can use the parent's fields through the relationship: Student__r.Name, Account.Industry. Relationship names let reports and queries "span" objects.

The child's related list appears on the parent's page layout once the relationship exists. You choose its columns and buttons in the page layout editor. Lookups can also show related lists by adding them manually in the layout.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens to detail records when their master is deleted?

    Master-detail enforces cascade delete: deleting the master deletes its details.
  2. 2. Which relationship type supports roll-up summary fields on the parent?

    Roll-up summary fields can be created only on the master side of a master-detail relationship.
  3. 3. How do you model a many-to-many relationship?

    A junction object links two parents, each through its own master-detail field.
  4. 4. Does a master-detail child have its own Owner field?

    The detail record has no separate owner; its access follows the master.

Summary

  • A relationship field on the child points to the parent; lookup is loose and optional, master-detail is tight and cascades.
  • Master-detail enables roll-ups and inherits sharing; lookup suits independent ownership and standard objects.
  • A junction object with two master-detail fields models many-to-many.
  • Choose the type before loading data because converting later has conditions.

Next: Formula Fields and Roll-Up Summaries