Get Grandchild Object Field Values from Parent Object

1.6k views Asked by At

I'm a newbie at sales-force apex coding. I have 3 custom objects, Location(Parent) -> Group(child) - > Meeting(grand child). All are related to each other through Master detail relationships. I am trying to get 2 field values from the earliest Meeting Record that belongs to the Location.

So far I managed to get the get 2 field values from the earliest Meeting Record that belongs to the Business Object.

public List<Meeting__c> MeetingsList2 = [SELECT Name,
GPS_Meeting_Location__Latitude__s, GPS_Meeting_Location__Longitude__s FROM    
Meeting__c WHERE Group__c =:id ORDER BY Meeting_Date__c ASC LIMIT 1];

I am trying to get the same information obtained from the above query but this time I want to get the earliest Meeting (grand child) Record that belongs to the Group records (child) belonging to the Location Record from the Location Object (Parent object getting grandchild record field values)

Any help appreciated.

1

There are 1 answers

0
Raul On BEST ANSWER

You can try query as:

public List<Meeting__c> MeetingsList2 = [SELECT Name,
GPS_Meeting_Location__Latitude__s, GPS_Meeting_Location__Longitude__s FROM    
Meeting__c WHERE Group__c =:id AND Group__r.Location__c =:locationId 
ORDER BY Meeting_Date__c ASC LIMIT 1];

I assumed, there is a reference field to Location(Location__c) in Group object and you have Location record Id in variable locationId. FYI- __r is used to traverse/access to parent fields for a custom object.