+ Start a Discussion
Biswajit Nath 9Biswajit Nath 9 

Schema.ChildRelationship is not showing any result

Hi All,

I want to use the below code to get all the child relationships of an object(both custom and standard):

Schema.DescribeSObjectResult R = Account.SObjectType.getDescribe();
List<Schema.ChildRelationship> C = R.getChildRelationships();

and want to get the object API name of the relationships as a list of strings. I am not able to figure out how to achive this. Could anybody please help?
AnudeepAnudeep (Salesforce Developers) 
Hi Biswajit, 

Try this code and let me know if this helps. This is just a sample, please make changes as per your needs
List<String> relationshipStrings = new List<String>(); 

for (ChildRelationship relation : SObjectType.Account.getChildRelationships())

relatioshipStrings.add(relation.getRelationshipName());

If you find this information helpful, please mark this answer as Best. It may help others in the community 

Thanks, 
​​​​​​​Anudeep
Biswajit Nath 9Biswajit Nath 9
Hi Anudeep,

Thank you very much for your answer. But I have tried everything. I have tried to run your code from the developer console, from workbench, and also I have embedded the code in my class. but there is no result. My requiremnt is very simple. Suppose object a and object b has lookup relation to contact. And in order to delete the contact first I need to delete object a and object b record. So by apex coding I want to find the API name of the object a and object b so that I can use it in the batch class in order to create the query. 

Any idea will be of great help. Many thanks in advance!