Sub topic 10
Why roll-ups need a master-detail relationship
This chapter assumes you know what objects, fields, and records are, and that you can open the Object Manager. It also assumes you know that a relationship links a child record to a parent record.
The rule
A roll-up summary field can only be created on the master object of a master-detail relationship. It cannot be created on the parent side of a lookup relationship. If you open the field wizard on an object that has only lookup children, the Roll-Up Summary type is not offered.
What master-detail guarantees
A lookup is a loose link. A master-detail relationship is a tight one, and the platform enforces several rules on it:
- The master-detail field on the detail record is required. Every detail record has exactly one master.
- The detail record has no Owner field. It inherits ownership and sharing from its master.
- Deleting a master record also deletes its detail records.
- Detail records can move to a different master only if "Allow reparenting" is turned on for the field.
A roll-up needs these guarantees. The platform must know which child records belong to each parent at all times. With a lookup, a child can have no parent, can be reassigned freely, and can outlive the parent. A total over a set that loose is not reliable, and the platform cannot recalculate it safely whenever a child changes.
Setting up the relationship
Create the master-detail field on the child object. In the Object Manager, open the child object (for example Line_Item__c), choose Fields & Relationships, then New. Select Master-Detail Relationship and pick the parent object. The wizard also asks about reparenting and sharing access.
Once the field exists, the parent object can have roll-up summary fields. Each one summarizes the detail records through that relationship.
Master object: Invoice__c
Detail object: Line_Item__c
Relationship: Line_Item__c.Invoice__c (Master-Detail)
Roll-up allowed: yes, on Invoice__c onlyConverting an existing lookup
You can change a lookup field to master-detail, but every existing child record must already have a value in that lookup. Fill in any blanks first, or the conversion fails. Conversion also changes behavior: the field becomes required, child records lose their own owner, and sharing now follows the parent.
The reverse conversion has a restriction. You cannot turn a master-detail field back into a lookup while the master object has roll-up summary fields that depend on it. Delete those roll-ups first.
Limits to remember
An object can have at most two master-detail relationships. If you need a roll-up on a parent that uses a lookup and you cannot convert it, see the chapter on roll-up alternatives for lookup relationships.
Exercise
An Account has many custom Payment__c records linked through a lookup field. A teammate says they cannot find Roll-Up Summary as a field type on Account. Why, and what is the most direct fix if payments should always belong to an account?
Show answer
Roll-up summary fields are available only on the master side of a master-detail relationship, and a lookup does not qualify. The direct fix is to make sure every Payment__c has an Account value, then convert the lookup field to master-detail. After that, Account can have a roll-up summary field over its payments.