function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
daniel k 4daniel k 4 

Identifying all the child objects of a parent object

Hi All,

I have an object(standar/custom) and need to identify all the related objects(lookup) of that object.
Let's say I have Account object and this object might be related as lookup to many other objects, how can I find all those objects ?

Thanks,
D
Best Answer chosen by daniel k 4
Raj VakatiRaj Vakati
Use Dynami apex

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


For getting All Child objects:
Schema.DescribeSObjectResult R = Account.SObjectType.getDescribe();
for (Schema.ChildRelationship cr: R.getChildRelationships()) {
system.debug(‘====child object===’+cr.getChildSObject());
}

 

All Answers

Raj VakatiRaj Vakati
Use Dynami apex

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


For getting All Child objects:
Schema.DescribeSObjectResult R = Account.SObjectType.getDescribe();
for (Schema.ChildRelationship cr: R.getChildRelationships()) {
system.debug(‘====child object===’+cr.getChildSObject());
}

 
This was selected as the best answer
daniel k 4daniel k 4
Thank You Raj !
Vireak RenVireak Ren
How to Change Object Name =>ex" Account.SObjectType..." How to change Account to Contact By Parameters.Please Help me Thank U guy.
Raghava VamseeKrishnaRaghava VamseeKrishna
How to pass object name dynamically? I get object name as string, I want to convert it to sobject type and pass it.Any other way?
Kaushik Muhuri 9Kaushik Muhuri 9
A small change in @Raj's code:

Schema.DescribeSObjectResult R = Account.SObjectType.getDescribe();
for (Schema.ChildRelationship cr: R.getChildRelationships()) {
    if (cr.isRestrictedDelete()) {
        system.debug(‘====child object===’+cr.getChildSObject());
     }
}
Kaushik Muhuri 9Kaushik Muhuri 9
@Raghava VamseeKrishna

Schema.getGlobalDescribe().get(fObjectName).getDescribe();
(here fObjectName is the name of the object in string)