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
Sumant KuchipudiSumant Kuchipudi 

How can I get parent's parent record fields from a child query? Is it possible? like join in SQL

How can I get parent's parent record fields from a child query? Is it possible? like join in SQL
The following is relationship with objects
Book__c  has lookup field Shop__c
Shop__c has lookup field Account
Account has a externalID

I want to get Account's externalID with the Book__c ID.

to Continue my question, in SQL I'll use to get parent's parent record field with this
select acc.relID 
from BOA b​oa, Contact con, Account acc 
where acc.accID = con.accID 
and con.conID = boa.conID 
and boa.boaID='ABC12342323'
Please advice
 
Best Answer chosen by Sumant Kuchipudi
Charisse de BelenCharisse de Belen
Hello Sumant,

You can try something like this:
 
String externalId = [SELECT Shop__r.Account.externalID FROM Book__c WHERE Id = 'ABC12342323'].Shop__r.Account.externalID;

I hope this helps.

All Answers

Charisse de BelenCharisse de Belen
Hello Sumant,

You can try something like this:
 
String externalId = [SELECT Shop__r.Account.externalID FROM Book__c WHERE Id = 'ABC12342323'].Shop__r.Account.externalID;

I hope this helps.
This was selected as the best answer
Sumant KuchipudiSumant Kuchipudi
Hi Charisse
Yes, it helped
I was able to get CustomObject-->Opportunity-->Account--ExternalID as well which I want..
Thanks for the clue..
 
Charisse de BelenCharisse de Belen
I am glad it helped. Please select the Best Answer when you can so this question can be marked Solved. Thank you!