function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ) 

Get avaialble related lists for object

Hi,

how to get available related lists names for any object?


Thanks,
N.J
MiddhaMiddha
Try using getChildRelationship() method.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject_describe.htm#apex_Schema_DescribeSObjectResult_getChildRelationships
Anoop yadavAnoop yadav
Hi,

Use parent to child soql query.

For more information. Check the below link.
http://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/Content/sforce_api_calls_soql_relationships.htm
srlawr uksrlawr uk
Using the Schema library you can get all the Child Relationships on an object, with something along the lines of (for Account sObject)

Schema.DescribeSObjectResult describeResult = Account.getSObjectType().getDescribe();
List<Schema.ChildRelationship> childRelationships = describeResult.getChildRelationships();


However, it should be noted that this will return ALL CHILDREN of the object, and there are many of these that are not "related lists" to the actual object in question. It is (unfortunatly) almost impossible to determine which ones are valid related lists (and certainly which are actually displayed on any page layouts).

The closest you might be able to get is to check if the relationship has a name (because the Related List children require a name) using something like

        for(Schema.ChildRelationship thisChild : childRelationships)
        {
            if(thisChild.getRelationshipName() != null)
            {
                // do something like add it to your result list
            }
        }

but this WILL still return some relationships that are not related lists/valid for use in the related list visualforce tag (was that a lucky guess?! ;) )
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Hey srlawr uk,

I did the same way but facing issue with not valid ralationships.
Thats why wanted to check if anyone knows how to get the relatedlist names which are available to use on object.

Thanks,
N.J
srlawr uksrlawr uk
Yep, this is something we spent hours on at my work, we could find no way to specifically query for just the valid children of an object for use as a related list. In the end, our output (a piece of dynamic apex) simply had to try to construct the visualforce and then catch an exception if an "invalid" child was applied to the tag - before spitting out a tidy message informing the user that relationship couldn't be rendered... :-S

Thinning out the list on Name got us pretty close. In the end, the requirement was revoked so we never polished it properly. You could possibly load the short list, and then quickly (and silently) spin through it in your controller trying to construct a dynamic related list component (which you subsequently just discard) and add all the non-exception throwing values into a list... but uggh, that sounds pretty gross!!