Skip to content

Instantly share code, notes, and snippets.

@Sunil02kumar
Last active May 30, 2019 02:24
Show Gist options
  • Select an option

  • Save Sunil02kumar/0faf96afb2f300eaf225be431578afe9 to your computer and use it in GitHub Desktop.

Select an option

Save Sunil02kumar/0faf96afb2f300eaf225be431578afe9 to your computer and use it in GitHub Desktop.
How to get all Child Relationship Names related to Parent Object using Apex
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<Schema.ChildRelationship> 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('*********childFieldToken:'+ childFieldToken);
system.debug('*********childObjectName:'+childObjectName);
system.debug('*********RelationshipName:'+RelationshipName);
objectRelationshipMap.put(childObjectName+'.'+childFieldToken,RelationshipName);
}
}
}
system.debug('***objectRelationshipMap.size():'+objectRelationshipMap.size());
system.debug('***objectRelationshipMap:'+objectRelationshipMap);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment