How to get all Parent and child objects related to the current object-soql query

Soql qury to fetch all Parent objects API names:

for(Schema.SobjectField fldMap: Account.SobjectType.getDescribe().fields.getMap().Values())
{
    if(fldMap.getDescribe().getType() == Schema.DisplayType.REFERENCE)
    {
        system.debug('Parent object Api names=====>' +fldMap.getDescribe().getReferenceTo());
    }
}

Soql qury to fetch all Child objects API names:

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

change on  API Name Account with your required object API name.

Post a Comment