Say I have a master-detail relationship between two objects, Schematic__c
and Part__c
.
I can SOQL query the master from the detail: [SELECT Schematic__r.Name FROM Part__c]
If I'm building a query from strings, I resolve the schema name, for example in a managed package:
SObjectType.Part__c.Fields.Schematic__c.Name //gives me 'ns__Schematic__c'
But when traversing into the master object, the __r
relationship suffix is what I need to surface. I've toyed around with the obvious Schema.SObjectType variations but I'm not having much luck:
SObjectType.Part__c.Fields.Schematic__r.Name //Schematic__r is not a field of ns__Symbol__c
What's the correct way to resolve the ns__Schematic__r.Name
token without replacing the suffix?
Best Answer
No no, the field (API) name is
Schematic__c
, you need to start from it. Think aboutSchematic__r
(the rel. name) like a consistent table alias when you'd make JOINs in regular database, nothing more.To go "up":
(You could also play with checking
f.getType() == Schema.DisplayType.REFERENCE
if you need to describe all fields and wonder which ones are lookups)to go "down":