function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Thomas MillerThomas Miller 

Accessing Records Returned by SOQL Query W/ Inner Query

Hi all,

Quick question about how to utilize the data I'm getting back from my query. There seems to be some confusion on my side as to how to either make the type correct, because I must return a specific type, or maybe how to modify my query slightly so I am getting back the desired type. There is a lookup relationship between two objects, Review__c and Media__c, the latter being the parent. The query functions in its current state, but I am completely at a loss as to how I might access the data in the "Reviews__r" column, column 2, of said query.

SOQL Query
 
List<Media__c> Med = new List<Media__c>();

				Med = [SELECT id, (SELECT Review_Submission_Date__c, 
				Comments__c, Media__c, Member__c, Rating__c FROM Reviews__r) FROM Media__c  
				WHERE id =:recId LIMIT 50];

The above is my query, and it will return a single row. I need the data contined in row 1, column 2, but am unsure how to extract it from the above List<Media__c> and place into a List<Review__c> type to be returned by my getter. If any clarification is needed to help solve this, just say so. Any help is much appreciated!

Best,
Tom

 
Best Answer chosen by Thomas Miller
Thomas MillerThomas Miller
I swear I could stare at the screen for hours and not find the answer, but as soon as I go ask for help on the forums it, I'll happen to find the correct google search of a related issue, and solve my own.

Here's the way to do this if anyone else is wondering:
 
List<Media__c> Med = new List<Media__c>();

				Med = [SELECT id, (SELECT Review_Submission_Date__c, 
				Comments__c, Media__c, Member__c, Rating__c FROM Reviews__r) FROM Media__c  
				WHERE id =:recId LIMIT 50];
				
List<Review__c> Rev = new List<Review__c>();

//This record will now have the correct type to be added to the list
				Rev.add(Med[0].Reviews__r[0]);

 

All Answers

Thomas MillerThomas Miller
I swear I could stare at the screen for hours and not find the answer, but as soon as I go ask for help on the forums it, I'll happen to find the correct google search of a related issue, and solve my own.

Here's the way to do this if anyone else is wondering:
 
List<Media__c> Med = new List<Media__c>();

				Med = [SELECT id, (SELECT Review_Submission_Date__c, 
				Comments__c, Media__c, Member__c, Rating__c FROM Reviews__r) FROM Media__c  
				WHERE id =:recId LIMIT 50];
				
List<Review__c> Rev = new List<Review__c>();

//This record will now have the correct type to be added to the list
				Rev.add(Med[0].Reviews__r[0]);

 
This was selected as the best answer
James LoghryJames Loghry

Correct.  If you need to iterate through all the Reviews for all the Media, per se, you could also do the following instead of accessing each indice individually:
 

for(Media__c media : 
    [Select
          Id
         ,(Select 
               Review_Submission_Date__c
               ,Comments__c
               ,Media__c
               ,Member__c
               ,Rating__c 
           From 
               Reviews__r) 
      From
          Media__c 
      Where 
          Id =:recId 
      Limit 50]{

    //If you want to do grab the entire list of reviews you could do:
    List<Review__c> currentReviews = media.Reviews__r
    
    //If you want to iterate through each of the reviews you could do:
    for(Review__c review : media.Reviews__r){
        //Do stuff
    }
}