13

I have a string which contain the name of a sobject. Now I want to get all the objects related to this object. Also want to get which kind of relationship it have with these object.

2
  • you want all the child objects?
    – Ratan Paul
    Apr 7, 2016 at 10:01
  • all child and the parent too. i.e all the object which are related to the current object.
    – Anu
    Apr 7, 2016 at 10:02

1 Answer 1

28

Get All Parent objects.

for(Schema.SobjectField strFld: Account.SobjectType.getDescribe().fields.getMap().Values())
{
    if(strFld.getDescribe().getType() == Schema.DisplayType.REFERENCE)
    {
        system.debug('==parent object='+strFld.getDescribe().getReferenceTo());
    } 
}

Get All Child objects.

Schema.DescribeSObjectResult R = Account.SObjectType.getDescribe();
for (Schema.ChildRelationship cr: R.getChildRelationships()) 
{
  system.debug('====child object==='+cr.getChildSObject());
}

Note: Based on your requirement just use your object API Name instead Account.

You must log in to answer this question.

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