4

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 1

5

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.

7
  • 1
    Note 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 F
    Commented May 13, 2022 at 14:43
  • @DerekF Good point. I've edited that in.
    – sfdcfox
    Commented May 13, 2022 at 14:47
  • @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 F
    Commented 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .