Salesforce script to fetch child objects of an object
When we work with objects in salesforce, sometimes we need a few things dynamically like child objects of an object. So I have created a script that gives us all the child objects of a given object.
String parentObjectName = 'Account';
Map<string,string> objectRelationshipMap = new Map<string,string>();
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
for(String ss1: schemaMap.keyset()){
Schema.SObjectType objToken=schemaMap.get(ss1);
if(ss1.equalsignorecase(parentObjectName)){
//find details about sobject
Schema.DescribeSObjectResult objDescribe=objToken.getdescribe();
List childRelationshipList = objDescribe.getChildRelationships();
system.debug('******childRelationshipList.size():'+childRelationshipList.size());
for(Schema.ChildRelationship ss:childRelationshipList){
string RelationshipName=ss.getRelationshipName();
string childObjectName = string.valueof(ss.getChildSObject());
string childFieldToken = string.valueof(ss.getField());
system.debug('*********child Field API:'+ childFieldToken);
system.debug('*********child Object Name:'+childObjectName);
system.debug('*********Relationship Name:'+RelationshipName);
objectRelationshipMap.put(childObjectName+'.'+childFieldToken,RelationshipName);
}
}
}
system.debug('***objectRelationshipMap.size():'+objectRelationshipMap.size());
system.debug('***objectRelationshipMap:'+objectRelationshipMap);







Leave A Comment