What I've done is queried the list of opportunities from my developer account and now what I'm trying to do is access the child object of one opportunity. Is there any possible way of doing this without doing any more SOQL queries?

I was thinking that since I have the list of opportunities, I would be able to move down from that parent object and access the child objects. Not sure though

For example, I want to access the competitors of an opportunity and store these results


You can use the inner query to get related list of a Object.

You can verify the Relationship name using Workbench --> Info --> Standard and Custom Object -> Select and Object for which Relationship to be verified

    List<Opportunity> oppList = [Select Id,name, (Select Id From OpportunityTeamMembers),  (Select Id From tasks) From Opportunity where Id =: 'some Opp Id'];

    for(Opportunity obj : oppList){
        for(Task t : obj.Tasks){

enter image description here

  • 2
    It's also useful to know that the child query ends up as a List<Task> (in this case) within each result of the 'parent' object. This inner list of child records can be accessed by oppList[someIndex].Tasks or oppList[someIndex].get('Tasks'). Also, if your subquery is for a custom object, the relationship name to use in the subquery will contain '__r' – Derek F Aug 10 '16 at 18:43
  • 2
    Also, for those who don't know about the apex workbench: workbench.developerforce.com – Derek F Aug 10 '16 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.