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.

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.