Skip to main content

Master-Detail

Master-detail relationships within Salesforce take the concept of a standard lookup relationship and add a large amount of additional functionality. At its core, master-details are comprised of two halves, the master record, which the relationship points to, and the detail record(s), which actually have the relationship field on them.

danger

Master-details should never be used when the detail contains, or could contain, confidential information. One wrongly placed Master-detail could inadvertently expose this information to the public. If a user (internal or external) has access to the master, they have access to the detail records too.

Roll-up summary fields

Roll-up summary fields can be created on the master side of a master-detail relationship, and allow details about the detail records to be aggregated and displayed on the master record. This can be useful for quickly displaying information about the detail records, such as the number of records, the sum of a field, or the maximum value of a field. See here for details on creation.

When using roll-up summary fields, there are some considerations that must be accounted for:

  • Permissions: Users do not need access to the detail records to see the roll-up summary values
  • Validation rules: When a detail record is edited, it can cause any validation rules on the master record to be triggered, so they can affect the detail records
  • Automations: Updating a detail record causes automations on the master record to be run. This can cause unexpected behaviour if not accounted for
caution

While roll-up summary fields can provide a lot of quick functionality, the benefits of using them must be weighed against the sharing implications. Choosing a master-detail relationship for only the roll-up summary fields is usually an anti-pattern, which will lead to major issues in the future. Apex powered roll-ups, using aggregate queries, can easily replace roll-up summary fields without much additional development time.

Implications

There are several major implications of choosing a master-detail relationship - ensure all are considered before making the decision:

  • Ownership: The master record owns the detail records, and as such, the detail records inherit the sharing settings of the master record. This means that if a user has access to the master record, they will have access to all the detail records too
  • Cascade Delete: If a master record is deleted, all the detail records are deleted too. This can be a major issue if the detail records contain important information
  • Automations: Creating or updating a detail record causes the master record, if it has roll-up summary fields, to have its update events triggered. Consider the implications of this when building automations
tip

When in doubt, use a lookup field over a master-detail relationship.