+ Start a Discussion
Prema -Prema - 

how to know that which objects are the childs of contacts or any object. How to see that because I tried to understand through ERD but I didn't understand it. Please help me

Best Answer chosen by Prema -
Maharajan CMaharajan C
Hi Prema,

you can use the below schema class to get the child objects details based on parent:

just execute the below class in the debug you can see the list of childs:

Schema.DescribeSObjectResult R = Contact.SObjectType.getDescribe();    ///// use the any object instead of contact here
List<Schema.ChildRelationship> C = R.getChildRelationships(); 
integer i = 1;
for(Schema.ChildRelationship rel:C){

   if (rel.getChildSObject().getDescribe().fields.getMap().get('Name') != null)
{
    System.debug(i + '  @@@ Child Name  '+rel.getChildSObject());    
    // System.debug(i + '  @@@ Rel Name  '+rel.getRelationshipName());  
    i++; 
}     
    
}


https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Schema_ChildRelationship.htm#apex_Schema_ChildRelationship_methods

http://sfdcsrini.blogspot.com/2015/05/schemadescribesobjectresult-in-apex-and.html
https://salesforce.stackexchange.com/questions/119832/get-the-specified-child-relationship-name-from-parent
http://salesforce-walker.blogspot.com/2012/12/getting-child-relationshipnames-using.html


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj

All Answers

cvuyyurucvuyyuru
You can try from Schema Builder.
Prema -Prema -
Which objects i should drag in schema builder apart from contact.
Maharajan CMaharajan C
Hi Prema,

you can use the below schema class to get the child objects details based on parent:

just execute the below class in the debug you can see the list of childs:

Schema.DescribeSObjectResult R = Contact.SObjectType.getDescribe();    ///// use the any object instead of contact here
List<Schema.ChildRelationship> C = R.getChildRelationships(); 
integer i = 1;
for(Schema.ChildRelationship rel:C){

   if (rel.getChildSObject().getDescribe().fields.getMap().get('Name') != null)
{
    System.debug(i + '  @@@ Child Name  '+rel.getChildSObject());    
    // System.debug(i + '  @@@ Rel Name  '+rel.getRelationshipName());  
    i++; 
}     
    
}


https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Schema_ChildRelationship.htm#apex_Schema_ChildRelationship_methods

http://sfdcsrini.blogspot.com/2015/05/schemadescribesobjectresult-in-apex-and.html
https://salesforce.stackexchange.com/questions/119832/get-the-specified-child-relationship-name-from-parent
http://salesforce-walker.blogspot.com/2012/12/getting-child-relationshipnames-using.html


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj
This was selected as the best answer
Prema -Prema -
Hello Raj, I just ran that code but I think i am getting wrong results isn't it? because it shouldn't show the account and contact right? [image: image.png]
Maharajan CMaharajan C
No it will work i ran the same code for Account it shows all the child objects [std obj + custom Obj]  for Account

User-added image
Prema -Prema -
But why it's showing accounts, contacts inside the debug? Any idea and if so then how do i know that it's the standard child object of contact.
cvuyyurucvuyyuru
Hi Prema,
Maharajan has given you the perfect solution.
Anything with out '__c' is to be considered as Standard Object.
Prema -Prema -
OK but m confused here. I should follow which object as child object of contact because it's showing account as well..although account is the parent not the child
Maharajan CMaharajan C
Hi Prema,

In Contact we have the standard field  called ReportsTo which is the self relation to contact itself so you have the Contact in debug and  please check the Account object did you have any Lookup field to Contact? surely you have the field thats the reason you have it also in debug.

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj
Prema -Prema -
Yes you are right I got it. Thank you so much.