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
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++; }
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++; }
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]
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
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!!!
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
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
Maharajan has given you the perfect solution.
Anything with out '__c' is to be considered as Standard Object.
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