Home » » How to get all Parent and child objects related to the current object

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

Written By Venu Gutta Salesforce LWC on Sunday, 11 February 2018 | 07:02

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());
    }
}

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.

0 Comments :

Post a comment