5

I've searched high and low, and I can't seem to figure out the Apex syntax I need to use to access the Related List Label for a Lookup relationship.

It shows up in the objectTranslation XML as relationshipLabel, but that text doesn't appear anywhere in the Apex documentation.

I had hoped that the following syntax would work:

Schema.SObjectType.Custom_Object__c.Fields.Custom_Field__c.relationshipLabel

But no luck:

Expression of type Schema.DescribeFieldResult has no member named relationshiplabel

Is that label accessible from Apex? I worry that it isn't, because I couldn't find it when browsing salesforce.schema in the Force.com IDE.

Update:

Someone has created an Idea to expose relationshipLabel in the WSDL, and I would expect it to extend to Apex as well.

1 Answer 1

6

I think we can get only API Names of the relationships...

Schema.DescribeSObjectResult R = Account.SObjectType.getDescribe();
List<Schema.ChildRelationship> C = R.getChildRelationships(); 

List<String> prettyPrint = new List<String>{''};
for(Schema.ChildRelationship cr : C){
    prettyPrint.add(cr.getChildSobject() + '.'+ cr.getField() + ' => Account.' + cr.getRelationshipName());
}

System.debug(String.join(prettyPrint, '\n'));

Account.ParentId => Account.null 
AccountContactRole.AccountId => Account.AccountContactRoles 
AccountFeed.ParentId => Account.Feeds 
AccountHistory.AccountId => Account.Histories
(...)
3
  • just an extra edit to @eyescream solution above: cr.getChildSobject().getDescribe().getLabelPlural() will fetch you Relationship Label (Plural form) for the Related Lookup Object.
    – VarunC
    Mar 13, 2013 at 20:15
  • 1
    @VarunC -- good tip -- except it actually gets the plural of the child Sobject's name which may or may not be "close" to the actual relationship name. For example, if child object is Foo__c with plural label Foos and the relationship label is Partner Foos, the code snippet returns only Foos. I concur with eyescream that this can't be done with the Apex Schema classes
    – cropredy
    Feb 13, 2016 at 0:08
  • yeah that's right :-)
    – VarunC
    Feb 15, 2016 at 14:00

You must log in to answer this question.

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