Skip to main content
4 of 4
formatting
crmprogdev
  • 41k
  • 9
  • 58
  • 116

How to get hierarchy of all objects dynamically through schema code

How to get a hierarchy of all objects dynamically through schema code in Salesforce I have code like below.

Set<String> strForRelatedObjects = new Set<String>();
Schema.SObjectType objectType = schema.getGlobalDescribe().get('child__c');

for(Schema.SobjectField strFld: objectType.getDescribe().fields.getMap().Values()){
    if(strFld.getDescribe().getType() == Schema.DisplayType.REFERENCE){
        strForRelatedObjects.add(string.valueof(strFld.getDescribe().getReferenceTo()).substringBetween('(',')'));         
    }
}

I want to fetch all parent object to object (Overall hierarchy). Above code should be able get all the related objects I want to fetch