SOQL Between Generational Hierarchy

Tzhe’ela Trooper
2 min readJul 16, 2022

I recently answered this question on the *Salesforce Developer* group in the trailhead community, hoping that this will also be available to my future self when I get stuck in a similar situation and don’t feel as creative as I was yesterday. However, the original question got deleted, and I only have this in an email thread. No chance my future self will remember it’s there… if I cannot google it, I will never see this again. The usual solution — write about it here, and hope that this piece of information will be found when needed!

Consider the following situation:
a. ObjectA is master of ObjectB
b. ObjectB is master of ObjectC
c. All three objects have a field called “Name”

Question 1: What will be the SOQL to fetch ObjectA__c.Name, when ObjectC__c.Id is given?

Question 2: What will be the SOQL to fetch ObjectC__c.Name, when ObjectA__c.Id is given?

Both really good questions, and since yesterday I felt creative, this was my answer:

Hi xxxx,
Given the following ERD :

Let’s say I have the ID of ObjectC__c as (a027R00000t5HkfQ) and I want to know what is the ID for the “grandparent” ObjectA__c.

You can use the following :

Let’s say I have the ID of ObjectA__c as (a007R000010S0LIQA0) and I want to know what is the ID for the “grandchild” ObjectC__c.

You can use the following :

Please let me know if this was helpfule and solved the questions you had.
Thanks

An answer with an ERD!!!
No way I’m keeping this in my inbox to never be found again.

You’re welcome future self ;)

--

--