Sub topic 8
Converting a Relationship and What Can Block It
This chapter assumes you can create lookup and master-detail fields in Object Manager. It also assumes you know how each type handles deletion, ownership, sharing, and roll-up summary fields, and that you can read basic SOQL. The earlier chapters on the two relationship types and on choosing between them cover this. Here you will learn how to change a relationship from one type to the other, what the change does to your data, and which conditions stop it.
Why you would convert
Requirements change after an org goes live. A few common cases:
- A lookup was chosen for flexibility, and the business now needs roll-up summary fields on the parent. Roll-ups require master-detail.
- A master-detail was chosen for tight coupling, and the business now needs child records that can exist without a parent, or that keep their own owner and sharing.
- A junction object needs a different primary master.
Salesforce supports conversion in both directions, but the two directions fail for different reasons. Check the blockers before you start.
What a conversion changes
A conversion does not just flip a label. It changes the child object's behavior in several ways.
| Behavior | Lookup | Master-detail |
|---|---|---|
| Parent value on child | Optional (unless you make it required) | Always required |
| Owner field on child | Present | Absent. The child takes the parent's owner. |
| Sharing for child | Its own sharing settings | Inherited from the parent |
| Deleting the parent | Child kept, or blocked, depending on the field's delete option | Child records are deleted with the parent |
| Roll-up summary on parent | Not available | Available |
Converting in either direction therefore alters who can see and edit records, not only how they are linked. Treat it as a security change as well as a schema change.
Lookup to master-detail
The main blocker: blank values
Every existing child record must have a value in the lookup field. If even one record has a blank parent, the conversion fails, because a master-detail field cannot be empty. Salesforce does not pick a parent for you.
Check for blanks first. Use a count query that reads the field on the child object. This example uses the Enrollment__c and Course__c objects from the many-to-many example.
SELECT COUNT()
FROM Enrollment__c
WHERE Course__c = nullIf the count is greater than zero, you have three options:
- Assign a real parent to each record.
- Assign a placeholder parent, such as a "Unassigned" course, then clean up later.
- Delete records that should not exist.
The anonymous Apex below does option 2. It assumes a placeholder Course__c named Unassigned already exists.
Course__c placeholder = [SELECT Id FROM Course__c WHERE Name = 'Unassigned' LIMIT 1];
List<Enrollment__c> orphans = [
SELECT Id, Course__c
FROM Enrollment__c
WHERE Course__c = null
LIMIT 10000
];
for (Enrollment__c e : orphans) {
e.Course__c = placeholder.Id;
}
update orphans;
System.debug('Updated ' + orphans.size() + ' records');This script handles at most 10,000 records per run. A single transaction can update at most 10,000 rows through DML, so a larger backlog needs repeated runs or a Batch Apex job. The query's LIMIT 10000 keeps the script inside that cap.
Other blockers for lookup to master-detail
Blank values are the most common problem, but not the only one. Check these as well:
- The child must be a custom object. A standard object can't be the detail side of a master-detail relationship. A lookup field on a standard object such as Contact or Opportunity can't be converted.
- The parent must be allowed to act as a master. Some standard objects, such as User, can't be masters.
- The object-level limits apply. A custom object can have at most two master-detail fields, and master-detail chains are limited in depth. If the conversion would exceed either limit, it fails.
- The relationship can't point to the same object. A master-detail field can't be a self-relationship. A lookup to its own object can't be converted.
Review the current Salesforce documentation for your release before a production change, because these limits and the list of eligible objects can change.
What happens to ownership and sharing
When the conversion succeeds, the child object loses its own Owner field. Each child record is now owned by the owner of its parent, and access follows the parent. Any sharing rules defined on the child object no longer apply. Two consequences are worth planning for:
- Users who could see a child record through its old owner or a sharing rule may lose access.
- Users who can see the parent may gain access to children they could not see before.
Before you convert in production, test with a sandbox copy and a few representative users.
Master-detail to lookup
The main blocker: roll-up summary fields
You can't convert a master-detail field to a lookup while the master object has roll-up summary fields that summarize the child object. A lookup can't support them. You must delete those roll-ups first, which also means finding any formulas, validation rules, or reports that reference them.
This is the reverse of the previous direction. You can lose calculated data here, so record what each roll-up computed and how you will replace it. The aggregate query from the earlier chapter is one replacement, run from a trigger or a scheduled job. A record-triggered flow is another.
Other points for master-detail to lookup
- After conversion the child gets an owner. The child object gains its own Owner field and its own sharing settings. Check the owner values on existing records and confirm they match what you intend.
- The field becomes optional. Decide whether the new lookup should be required, and set the behavior for parent deletion, either clear the field or block the delete. Children are no longer deleted with the parent. If you rely on cascade delete for cleanup, you will now need an automated process for it.
- Junction objects. If you convert one of the two master-detail fields on a junction object, the junction object keeps a single master-detail relationship. If you convert the primary master, the secondary master takes over the primary role. The object also stops being a true junction object, because it now has one master-detail and one lookup.
Deciding whether to convert
The diagram summarizes the checks in order.
Converting with metadata
In a source-driven project, the conversion is a change to the field's metadata file followed by a deployment. Here the field from the earlier chapters was a lookup, and it changes to master-detail.
<?xml version="1.0" encoding="UTF-8"?>
<!-- force-app/main/default/objects/Enrollment__c/fields/Course__c.field-meta.xml -->
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Course__c</fullName>
<label>Course</label>
<referenceTo>Course__c</referenceTo>
<relationshipLabel>Enrollments</relationshipLabel>
<relationshipName>Enrollments</relationshipName>
<relationshipOrder>0</relationshipOrder>
<reparentableMasterDetail>false</reparentableMasterDetail>
<type>MasterDetail</type>
<writeRequiresMasterRead>false</writeRequiresMasterRead>
</CustomField>When you change <type> from Lookup to MasterDetail, remove the lookup-only elements, such as <deleteConstraint>, and add the master-detail elements shown above. The deployment fails if any records in the target org have a blank value, so run the blank-value check in each org before you deploy to it. Sandbox data often differs from production, and a clean sandbox does not prove production is clean.
Common errors and fixes
- "Cannot convert: records have null values." Run the
WHERE Course__c = nullcount, fix the rows, and retry. - Conversion option not offered for a standard object. The object can't be a detail. Keep the lookup and enforce the requirement with a validation rule or a required field.
- Cannot convert to lookup because of roll-up summary fields. Delete or replace the roll-ups, and check formulas and reports that depend on them.
- Users lose access after conversion. Child sharing now follows the parent. Adjust parent sharing, or use a lookup with explicit sharing rules instead.
- Deployment succeeds in the sandbox and fails in production. Production has blank values the sandbox does not. Query production before you deploy.
Summary
- Lookup to master-detail needs a parent value on every child record. It also needs a custom child object, and the object limits must still hold.
- Master-detail to lookup needs all roll-up summary fields on the master removed first.
- Conversion changes ownership, sharing, and delete behavior, so test with real user profiles.
- In source-driven projects, check the data in every target org before deploying the changed field.
Exercise
Your org has a lookup field Project_c on the custom object Taskc. A count query shows 42 Taskc records with Projectc blank. You want to convert the field to master-detail. What must you do first, and what changes for Task_c after the conversion?
Show answer
Give the 42 records a parent first, by assigning a real Project_c, assigning a placeholder project, or deleting the records. A master-detail field can't be blank, so the conversion fails until no blanks remain. After conversion, Taskc no longer has its own Owner field. Each task is owned by the owner of its project and inherits the project's sharing, and any sharing rules on Task_c stop applying. Deleting a project also deletes its tasks.
Exercise
A master-detail field from InvoiceLinec to Invoicec needs to become a lookup. Invoicec has a roll-up summary field named TotalLines__c that counts the lines. What blocks the conversion, and what is a way to keep the count afterward?
Show answer
The roll-up summary field TotalLinesc blocks the conversion, because roll-ups only work with master-detail. Delete the roll-up, after removing any formulas or reports that use it, then convert. To keep the count, compute it another way, for example with an aggregate query such as SELECT Invoicec, COUNT(Id) FROM InvoiceLine_c GROUP BY Invoicec, run from a trigger, a scheduled job, or a record-triggered flow that writes the result to a number field on Invoice_c.