• Esteban Castro R
  • NEWBIE
  • 10 Points
  • Member since 2016
  • Experian

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Hi Team,

I wonder if there's a best practice regarding the following scenario for  going on a for loop inside a for loop for a SOQL query on an object with a sub query. For example:
List<Parent__c> lp = [
  SELECT Id, Name,
    (SELECT Id, Name FROM Child__r WHERE Name = :childFilter)
  FROM Parent__c
  WHERE Name = :parentFilter
];

for(Parent__c p : lp){
  System.debug('Parent__c Name: ' + p.Name);
​  for(Child__c c : lp.Child__r){
    System.debug('Child__c Name: ' + c.Name);
  }
}
Should I build the query on the child level and populate a Map<Id,Child__c> with back references to the parent object attributes, then do a for inside a for loop over map Id, then object? Thanks a lot.
Hi Team,

I wonder if there's a best practice regarding the following scenario for  going on a for loop inside a for loop for a SOQL query on an object with a sub query. For example:
List<Parent__c> lp = [
  SELECT Id, Name,
    (SELECT Id, Name FROM Child__r WHERE Name = :childFilter)
  FROM Parent__c
  WHERE Name = :parentFilter
];

for(Parent__c p : lp){
  System.debug('Parent__c Name: ' + p.Name);
​  for(Child__c c : lp.Child__r){
    System.debug('Child__c Name: ' + c.Name);
  }
}
Should I build the query on the child level and populate a Map<Id,Child__c> with back references to the parent object attributes, then do a for inside a for loop over map Id, then object? Thanks a lot.