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.
Asked
Active
Viewed 1.2k times
13
-
you want all the child objects? – Ratan Paul Apr 07 '16 at 10:01
-
all child and the parent too. i.e all the object which are related to the current object. – Anu Apr 07 '16 at 10:02
1 Answers
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.
Ratan Paul
- 22,663
- 13
- 52
- 97