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
Kelly Logan (Raptek)Kelly Logan (Raptek) 

SOQL querying multiple levels of master-detail related objects

We need to pull data from multiple objects that are related at different levels through master detail relationships for a sync with an external system. The top is the Contact object. In each case we want to pull data when anything in any of the related objects has been updated since the last sync (which we keep track of in a separate log object). 

Here is the list of relationships (Master -< Detail):
Contact -< Application -< Student Term -< Student Term Payment
Contact -< Application -< Appeal
Contact -< FAFSA Detail

My solution right now is to query each separately and present them in a set of lists that can be stitched together with their foreign keys. No processing needs to be done as a single object so this seems the most efficient (only pull the data that was modified, leave other related objects' data alone if it hasn't been changed) and the easiest. 

I am curious though - could this be done as a single query? I don't see how it could be, even if you started with Student Term as base because as far as I understand you can't go up one level Detail to Master then back down from the Master to a different Detail object. If there is a way to do this though I'd love to hear.

Alternately, is there a way to create something like an SQL View form that would encapsulate all these queries into a single queryable object? Like creating a multi-object report and then running it remotely through Apex and gathering the results for use?
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Kelly Logan,

May I request you please check the Salesforce documentation on  "SOQL Relationship Queries" check below link.  
Basic Examples of SOQL RELATIONSHIPS:


Child > Parent (Standard Object)
Selectid,Account.Name,Account.Phone,Account.industry,Account.Type,Account.Rating,Account.website,Account.Ownership,Account.AnnualRevenue,Account.NumberOfEmployees,Account.CleanStatus from Contact
Child >Parent(Custom Object)
 
Selectid,COLLEGE__r.Name,COLLEGE__r.Contact__c,COLLEGE__r.Count__c,COLLEGE__r.Highest_Marks__c,COLLEGE__r.Address__cfrom Studnt__c

Parent >Child(Standard object)
select Name, Industry, (select AssistantName, Email from contacts)from ACCOUNT

Parent >Child (Custom Object)
1select id,Name,(select Studnt__c.name__c from Studnts__r) from College__C

I hope it will be helpful.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Best Regards
Rahul Kumar

 
Kelly Logan (Raptek)Kelly Logan (Raptek)
That is useful background info, Rahul, but the question is about multiple levels of SOQL in the same query. Can all six objects be part of the same query? That is what will answer the question.
J BaylesJ Bayles
If I remember right, you may only traverse 3 objects per query. 
J BaylesJ Bayles
You’ll also want to remember that you can only go one level down in relationships when querying from parent to child and five levels up the relationship chain when querying from a child to a parent.