Skip to main content
1 of 4

How to get hirarchy of all objects dynamically through schema code in salesforce

How to get hirarchy of all objects dynamically through schema code in salesforce I have code like below.

set strForRelatedObjects = new set();

  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 hirarchy).and above code should in position to get all the related objects to to objects i want to fetch