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
Gaurav SuryawanshiGaurav Suryawanshi 

Retrieving the Account Parent Child hierarchy

Hello Everyone,  I am new Here
I would like to retrive a parent child hierarchy.

I have 4 accounts.

Account1
      |_____>Account 2
                          |_____>Account 3
                                             |_______>Account 4

I want to retrive this parent child hierarchy by using query.

How to do??
Thanks in advance
 
Best Answer chosen by Gaurav Suryawanshi
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Gaurav,

May I request you please check the Salesforce documentation on  
"SOQL Relationship Queries".

https://developer.salesforce.com/blogs/developer-relations/2013/05/basic-soql-relationship-queries.html
 
Basic Examples of SOQL RELATIONSHIPS:
Child > Parent (Standard Object)
 
Selectid,Account.Name,Account.Phone,Account.industry,Account.Type,Account.Rating,Account.website,Account.Ownership,Account.AnnualRevenue,Account.NumberOfEmployees,Account.CleanStatus from Contact

Child >Parent(Custom Object)
 
Selectid,COLLEGE__r.Name,COLLEGE__r.Contact__c,COLLEGE__r.Count__c,COLLEGE__r.Highest_Marks__c,COLLEGE__r.Address__cfrom Studnt__c

Parent >Child(Standard object)
select Name, Industry, (select AssistantName, Email from contacts)from ACCOUNT

Parent >Child (Custom Object)
select id,Name,(select Studnt__c.name__c from Studnts__r) from College__C

I hope it will be helpful.

Best Regards
RahulKumar
 

All Answers

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Gaurav,

May I request you please check the Salesforce documentation on  
"SOQL Relationship Queries".

https://developer.salesforce.com/blogs/developer-relations/2013/05/basic-soql-relationship-queries.html
 
Basic Examples of SOQL RELATIONSHIPS:
Child > Parent (Standard Object)
 
Selectid,Account.Name,Account.Phone,Account.industry,Account.Type,Account.Rating,Account.website,Account.Ownership,Account.AnnualRevenue,Account.NumberOfEmployees,Account.CleanStatus from Contact

Child >Parent(Custom Object)
 
Selectid,COLLEGE__r.Name,COLLEGE__r.Contact__c,COLLEGE__r.Count__c,COLLEGE__r.Highest_Marks__c,COLLEGE__r.Address__cfrom Studnt__c

Parent >Child(Standard object)
select Name, Industry, (select AssistantName, Email from contacts)from ACCOUNT

Parent >Child (Custom Object)
select id,Name,(select Studnt__c.name__c from Studnts__r) from College__C

I hope it will be helpful.

Best Regards
RahulKumar
 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Please try below query
select name,
       parent.Name,
       parent.parent.Name,
       parent.parent.parent.Name  
       from account limit 1

Let us know if this will help you

 
Shiva RajendranShiva Rajendran
Hi Gaurav Suryawanshi,
Lets say you have the id of the first account ,then use the below code to get all the four associated account records
Id firstAccountId='0012800001IjcB1';
List<account> ParentToChildHierarchy=new List<Account>();

while(true)
{

   Account acc=[select id,accountId from account where id=:firstAccountId];
    ParentToChildHierarchy.add(acc);
   if(acc.accountId!=null)
     {
        firstAccountId=acc.accountId;
      }
else
  break;

}

for(Account acc:ParentToChildHierarchy)
{
   System.debug(acc.id +'  ' + acc.name);
}
Let me know if you need further help.


Thanks and Regards,
Shiva RV