You need to sign in to do that
Don't have an account?
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?
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?
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) Child >Parent(Custom Object)
Parent >Child(Standard object) Parent >Child (Custom Object)
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