i have 2 object one is Master and another one is Detail object, and i need to get related list record from parent query. so i have written inner query.i know how to iterate inner query results using 2 for loops. but i dont want to use 2 for loops .can anyone help in this?

  • Are you saying that you want to iterate over the children but have access to the related parent as well?
    – dphil
    Jun 14, 2017 at 20:00
  • AS your requirement is to get related list record from parent query. , you can only get a list of children from the parent query and not individual records. So you have to use a For loop to iterate over each child. May be you can post what you tried and we can see if something is wrong. Why not consider querying children based on parentid if you are concerned about nested for loops?
    – SfdcBat
    Jun 14, 2017 at 20:27

1 Answer 1


Using Maps to get associated objects. In this case using the relationship from the child to get the parent.

Map<Id, SObject> parentObjMap = new Map<Id, SOBject>([SELECT Name, OtherValues__c FROM SObject WHERE SOME_CRITERIA]);

for (SObjectChild child: childObjects)
  Sobject parent = parentObjMap.get(child.ParentRelationship__c);

  //Do Work

This only has a single loop and gives you access to both the parent/child. Without more info you can't really get anything better than this.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .