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

Can't access data obtained in a nested query

I have this query


List <Booking__C> bk = [Select (Select Contact__c From Pupils__r) From Booking__c b where = :bookingId];


This query should return me the related Contact ID's from Pupils sObject which is a Child Object for Booking and I'm saving the result in a List of type Booking__c, but how do I access the result of related records in another query, if I want to run another query with the results of this one


For Eg:


List <contact> con = [select firstname, lastname, id from contact where id = ??? (I have tried bk.Contact__C, also tried - nothing works, it comes up with an error)


I'm new to salesforce can't get my head around accessing nested query results.


I'll be glad if any one can tell me where I'm going wrong.



  1. List<Pupils__c> ll = bk.get(0).Pupils__c

 This will give u the pupils for the first record of bookings. Use a for loop to get all the pupils.



That link is good to understand the nested / relationship queries.