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
MaheemSamMaheemSam 

How to get grand parent

Hi,
  
 I have a below query which will display parent, grand parent till top 5 grand parent can we do this using apex which should automatically display parents till top parent Please suggest me
select id, Name,
                                     ParentId,
                                     Parent.ParentId,
                                     Parent.Parent.ParentId,
                                     Parent.Parent.Parent.ParentId,
                                     Parent.Parent.Parent.Parent.ParentId                                     
                                     from 
                                        Account
                                     where 
                                        ParentId != null
                                        and
                                        id = '0013400001S5ZTm'

Thanks
Sudhir
 
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Sudhir,

Accessing grandparent through SOQL.please refer the below link for reference. 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.​

Thanks
Rahul Kumar
 
MaheemSamMaheemSam
Hi Rahul,

  Thanks for you reply I already have it in a SOQL I need this something in APEX where just passing a account id I need all the garand parent list to be displayed till the top level let me know if you can help in code. 

Thanks
Sudhir
Sharad SoniSharad Soni
Hello Sudhir,

I have a custom Object Expense Item Which having the parent in the below hierarchy...
Select Id  , Expense__c , Expense__r.Member__r , Expense__r.Member__r.Person__r , Expense__r.Member__r.Person__r.House__r FROM 
Expense_Item__c WHERE Expense__c != null AND Id =:''\0013400001S5ZTm'.



Mark, it as best answers so that this question will be filtered out from unsolved questions.
Thanks
Regards Sharad
Sharad SoniSharad Soni
TypoCorrection

WHERE Id =:'0013400001S5ZTm'.