Skip to main content

Master-Detail vs Lookup in Salesforce

Master-Detail vs Lookup

💬 In plain words:  Master-Detail is a parent-child bond: child dies with the parent, inherits its sharing, and enables roll-up summaries. Lookup is a loose reference: both records live independently. Choose MD when the child has no meaning alone; choose lookup when it does.

📌 Example:  Invoice Line without an Invoice is meaningless → Master-Detail: delete the invoice, lines go too, and 'Invoice Total' is a free roll-up. Contact to 'Preferred Hotel' → Lookup: hotel deleted, contact lives on.

🎬 Real-Life Example: The Roll-Up That Cost a Sprint  Every Route__c screen must show how many Delivery__c records it carries. Simple total. Live at all times.

The Old/Bad Way:  The team built Route–Delivery as a Lookup. Lookups have no roll-up summary fields. So they wrote a trigger: count children on insert, subtract on delete, recount on reparent, handle undelete, handle bulk loads.

Why this is bad:  You are rebuilding, by hand, a feature the platform gives away free. Every missed edge case leaves a wrong total on a manager's screen. And wrong totals destroy trust fast.

The New/Good Way:  1. Ask the real question first: is a Delivery meaningless without its Route? Yes. Should deleting a Route delete its Deliveries? Yes.  2. Then Master-Detail is the honest answer.  3. Add a Roll-Up Summary field: COUNT of Deliveries. The platform keeps it correct through delete, undelete, and reparent.

The payoff:  Pick the relationship for the behavior you need — ownership, cascade delete, roll-ups — not for the line on the diagram.

Concept

Master-Detail (MD) makes the child depend on the parent. The child inherits the parent's owner and sharing. Delete the parent and the child goes with it. And you get Roll-Up Summary fields on the master: COUNT, SUM, MIN, MAX. A Lookup is a loose reference. The child keeps its own ownership and sharing. The field can be optional. There is no cascade delete — you only get 'delete this record also' through lookup filter behaviors. And there are no native roll-ups. This one choice drives your sharing design in Module 3, your roll-up strategy, and your delete behavior. It is structural, and it is hard to reverse later.

🧠 Pick MD when: child is dependent, shares parent's security, needs roll-ups. Pick Lookup when: independent life, own sharing, optional link. 

🧭 360 Card — Master-Detail vs Lookup 

Rule:  Master-Detail when the child cannot exist alone. Lookup when it can.

Gain:  cascade delete, roll-up summary fields, inherited owner and sharing, required parent. Four things, free.

Price:  coupling. The child gives up its own owner, its own sharing, and its own life.

Limits:  two Master-Details per child. Reparenting is off by default. Twenty-five roll-up summary fields per parent. Forty lookups per object.

Mirror — Lookup:  optional link, own owner, own sharing, no native roll-ups, no cascade delete.

Later:  junction objects need both Master-Detail slots. Spend one here and many-to-many is gone (2.2).

At volume:  every child save locks the parent. Ten thousand children under one parent is skew, and saves start queueing (10.6).

Core Q&A

Q: How do you decide between Master-Detail and Lookup for a new relationship?

🎯 Say this first:  Master-Detail if the child can't exist alone and should inherit parent sharing (and you want roll-ups); Lookup for everything looser.

A: Ask three questions. One: does the child mean anything without the parent? If not, use Master-Detail. Two: should the child's visibility follow the parent's? Master-Detail inherits sharing, so if the child needs its own OWD and its own sharing rules, use a Lookup. Three: do you need declarative roll-ups? Native Roll-Up Summaries need Master-Detail. With a Lookup you fall back to Flow, Apex, or a DLRS-style rollup. Then weigh the limits. An object can have at most 2 Master-Detail relationships, and the child of one MD cannot be the master of another MD chain beyond a limited depth.

Follow-ups (scenario-based)

Q1: You need to convert an existing Lookup to Master-Detail on an object with 2M records. What are the preconditions and the risks?

A1: Two things must be true before you convert. Every child record needs a non-null parent — no orphans. And you must accept that ownership on the child is discarded. Children take the parent's owner and sharing from then on, so any owner-based report, sharing rules, or assignment logic on the child breaks. At 2M records the sharing recalculation is heavy. Plan it like a migration: off-hours, with deferred sharing. Also check the roll-up limits on the parent before you promise roll-ups as the payoff. The cap is 25 roll-up summary fields, and the default is lower.

Q2: A child object needs roll-ups on the parent, but also needs independent sharing. What are your options?

A2: Keep the Lookup, so the child keeps its own sharing. Then compute the roll-up some other way. A record-triggered after-save Flow handles simple counts. Apex in the trigger handler handles anything that needs aggregate queries at volume. A scheduled batch recalculation handles very hot objects, where an eventually-consistent total is good enough. Say the trade-off out loud: real-time roll-ups in a trigger cost SOQL and DML on every save, while batch roll-ups cost freshness. Common mistake: switching to Master-Detail just to get roll-ups, and breaking the security model to do it.


Popular Posts

Must-listen songs for developers

Here are some must-listen songs for developers: "Strobe" by deadmau5 . This electronic dance music (EDM) track is perfect for getting into a flow state. The repetitive beat and simple melody are easy to focus on, and the overall mood of the song is upbeat and motivating.  "Viva la Vida" by Coldplay . This rock song has a soaring melody and powerful lyrics that can inspire you to stay focused and productive. The song's message of hope and resilience is perfect for those times when you're feeling stuck or discouraged.  "Code Monkey" by Jonathan Coulton . This tongue-in-cheek song is a hilarious and accurate portrayal of the life of a software developer. The lyrics are catchy and the song's upbeat tempo will make you want to get up and dance.  "The Sound of Silence" by Simon & Garfunkel . This classic folk song is perfect for those times when you need to focus and concentrate. The song's slow tempo and haunting melody will h...

Apex Test Class Examples for @HttpPost Exposed WebService Class

Introduction: In Salesforce, the Apex programming language allows you to create powerful web services that can be exposed to external systems for data integration. One common scenario is using the @HttpPost annotation to create a custom RESTful web service. In this blog post, we'll walk through some examples of how to write effective test classes for an @HttpPost exposed web service class in Salesforce. Writing comprehensive test classes ensures that your code is robust, functional, and ready for deployment.

Salesforce LWC Code for Multi-Select Lookup

Introduction: In Salesforce Lightning Web Components (LWC), implementing a multi-select lookup field can enhance the user experience and provide greater flexibility for selecting multiple related records. In this blog post, we will walk through the process of creating a multi-select lookup field using LWC. We will cover the required code snippets and provide step-by-step instructions to help you implement this functionality in your Salesforce org.

Uninstall all Windows 10 default apps using Powershell

Here is script to uninstall all windows 10 default modern apps. This script uninstalls xbox, xbox Game bar, Xbox App,Xbox Gaming Overlay, Get started etc from your computer. No need to run one by one commands Just copy below script, run  powershell as administrator and paste script and press enter . It will automatically uninstall all default programs.  If you do not  want to uninstall some apps than just remove " "  line from script. $packages = @( "7EE7776C.LinkedInforWindows" "C27EB4BA.DropboxOEM" "Microsoft.3DBuilder" "Microsoft.Microsoft3DViewer"

How to Save Quote PDF, Send PDF, Preview PDF in salesforce with custom functionality

Want to develop custom pdf viewer, save pdf in quote pdf related List and Send quote to customer on button click when quote is custom in salesforce . These functionality are standard from salesforce. but you can develop these functionality custom in salesforce. Here is the solution:- Custom button to save Quote PDF and send PDF  Step 1:-  First Create Two custom button. which will used for PDF preview and Save quote pdf in quotes pdf related list.                               1. PDF preview Button                              2. Save & Send Quote Button Replace "Your VF page here" to Your quote PDF cuatom page. Step 2:-  PDF preview button   pdf preview button will display the pdf's preview in standard format of salesforce. So you need to set the  following configuration (In picture). After that you have ...

Setup vs Non-Setup Objects (Mixed DML)

Core Platform & Fundamentals Module map MODULE 1 root: 'Who can do what, on which objects/fields?' ├─ Setup vs non-setup (transaction rules) ├─ Profile + Perm Sets + PSG (access = union) ├─ Licensing (the ceiling) ├─ CRUD/FLS (objects & fields) └─ Reports/Dashboards (who sees what data) 1.1 Setup vs Non-Setup Objects (Mixed DML) 💬 In plain words:   Salesforce keeps 'admin' records (like User, Group) and 'business' records (like Account, Case) in two separate rooms. One transaction cannot write to both rooms at once — that error is Mixed DML. The fix: do the second write in a separate async step. Concept

Unveiling the Power of Named Credentials in Salesforce with Comprehensive Code Examples

Introduction: Named Credentials are a powerful feature in Salesforce that allow you to securely authenticate and connect to external services and APIs without exposing sensitive information like usernames and passwords. In this blog post, we'll delve into the world of Named Credentials, understand their significance, and provide you with in-depth code examples to illustrate their implementation in various scenarios.