Is it possible to check using Apex, is relationship field type of lookup or master-detail? Schema.DescribeFieldResult.getType()
for both of them returns simply 'REFERENCE'
1 Answer
getRelationshipOrder
will be null for a lookup relationship, but will be 0 or 1 for a master-detail relationship.
Edit: I second-guessed myself, as I was mobile, but I have confirmed that getRelationshipOrder
is only populated for master-detail relationships, and null otherwise.
Edit2: Some standard "master-detail" relationships also return null, so you may need a "white-list" of known false negatives.
-
1Note that the result will also be
null
for at least some standard master-detail relationships (OpportunityId on OLI, and QuoteId on QLI are ones I've confirmed)– Derek FCommented May 13, 2022 at 14:43 -
-
@DerekF I've checked that OpportunityId on OLI is a lookup. Can it vary depending on some conditions? Commented May 13, 2022 at 14:59
-
1@MaciejKról OpportunityId is not a lookup. OpportunityLineItems cannot exist without an Opportunity, OLI has no
OwnerId
field (ownership and sharing are determined by the "Master" record), and you can create a rollup summary of OLIs on Opportunity. All of those are hallmarks of an M-D relationship. Though with it being a standard field and Salesforce being, well... Salesforce, I wouldn't be surprised if they broke their own rules/conventions.– Derek FCommented May 13, 2022 at 15:03 -
2@Elijah See CustomObject: 0 is the primary parent, 1 is the secondary parent. If there is only one M-D relationship, that relationship must be set to 0. All lookup fields must not specify a value (it must be null).– sfdcfox ♦Commented May 13, 2022 at 17:15