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
Nishchal Vashisht 1Nishchal Vashisht 1 

How to extract Account.Parent.Parent field value from SOQL Query

This is my query.
SOBject acc = [Select Id, Name, (Select Id, Account.Parent.Parent.Name, Name from Contacts__r) from Sales_Location__c where Id ='a005j000009xxxBAxU']; 
Contact abc =(Contact)(acc.getSobjects('Contacts__r')).get(0); System.debug(abc.get('Account.Parent.Parent.Name'));

But instead of using get() if I  directly use abc.Account.Parent.Parent.Name , I am getting result. But I don't know why  my code fails in case of get().  In Actual scenario abc is of SObject type that's why I need to use get() here.

 
Rohit Kumar SainiRohit Kumar Saini
Hi

You can get the related object fields like this
 
System.debug(abc.getSobject('Account').getSobject('Parent').getSobject('Parent').get('Name'));

Thanks
VinayVinay (Salesforce Developers) 
Hi Nishchal,

Hope your query is answered below.

https://stackoverflow.com/questions/71908611/how-to-extract-parent-parent-field-value-from-soql-query

Thanks,