+ Start a Discussion
Lee_CampbellLee_Campbell 

Generate "List" of Child Objects

Hi all,

 

Does anyone know of a way of producing a "list" of objects that are children of a given object? For example, if I have an object that is the parent in a master-detail relationship and that object has many objects at the "detail" end of the relationship, is there a way I can list these detail objects? I don't mind what format the list is in (string, string[], List<>, it's trivial to sort that out, isn't it?), I'd just like a way of querying, programmatically, for all of the objects that are children of a given object. I've RTFM, but I can't seem to see how to do this.

 

Thanks in advance, lovely people of Developerforce!

 

Lee

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma
Hi Lee,

I am working on very similar functionality where I am customizing standard clone functionality. Using this, I would be able to clone account object along with it's all child object data. In this functionality, I am doing the same thing what you have posted here:
1. I look for the object.
2. Fetch all the ChildRelationship to it
3. Create clone for Parent
4. Create clone of Children
5. Commit data

There is a method called getChildRelationship. This will give you all the child objects name associated with Parent. Also you can get RelationshipField name. Like in case of Account and Contact, relationship name will be AccountId. You can use this Relathionship field name for querying the chilkdren.

All Answers

Bhawani SharmaBhawani Sharma
Hi Lee,

I am working on very similar functionality where I am customizing standard clone functionality. Using this, I would be able to clone account object along with it's all child object data. In this functionality, I am doing the same thing what you have posted here:
1. I look for the object.
2. Fetch all the ChildRelationship to it
3. Create clone for Parent
4. Create clone of Children
5. Commit data

There is a method called getChildRelationship. This will give you all the child objects name associated with Parent. Also you can get RelationshipField name. Like in case of Account and Contact, relationship name will be AccountId. You can use this Relathionship field name for querying the chilkdren.
This was selected as the best answer
Lee_CampbellLee_Campbell

Brilliant. Thank you.