+ Start a Discussion
swain 10swain 10 

Fetching Child Account ids from Parent Account

How to fetch all child account and frnd child account ids from a parent account Soql query? 
Best Answer chosen by swain 10
Akshay_DhimanAkshay_Dhiman
Hi Swain,

Use this code to fetch All child's according to your requirement 
 
public class totestChildAccount {
    public static void toChildAccount()
    {
        List<Account> acctList = new List<Account>();
        acctList = [Select name,Id from Account];
        List<Account> acctListParent = new List<Account>();
        for(Integer i =0; i<acctList.size();i++)
        {
            acctListParent = [Select  name,Id from Account where parentId =: acctList[i].id];
        }
        List<Account> acctFrndAccount = new List<Account>();
        acctFrndAccount =[Select Name,Id from Account where parentId != null];
        System.debug('All Accounts--->'+acctList);
        System.debug('All Childs Accounts-->'+acctListParent);
        System.debug('All frnd Accounts--->'+acctFrndAccount);
    }
}


please mark the best answer if it helps you.

Thanks
Akshay

All Answers

Raj VakatiRaj Vakati
You cannt able to get it single SOQL query .. You will get 
The inner and outer selects should not be on the same object type error .. 

use this code

 
Set<Id> parentId = new Map<Id, AggregateResult>([SELECT ParentId Id FROM Account WHERE ParentId != NULL GROUP BY ParentId]).keySet();
List<Account> acc = [select id from account where id not in : parentid];

Or
 
Set<Id> parentId = new Set<Id>();
for(AggregateResult a : [select parentid p from account group by parentid])
{
    if(a.get('p') != NULL)
    {
        parentId.add((Id)a.get('p'));
    }        
}
List<Account> acc = [select id from account where id not in : parentid];



 
swain 10swain 10
Hi Raj ,

Thanks a lot . But I need to get the childs of Account , on which Account page i am currently on.So I need to fetch the Id from that account and then using that id I need to Fetch.
Akshay_DhimanAkshay_Dhiman
Hi Swain,

Use this code to fetch All child's according to your requirement 
 
public class totestChildAccount {
    public static void toChildAccount()
    {
        List<Account> acctList = new List<Account>();
        acctList = [Select name,Id from Account];
        List<Account> acctListParent = new List<Account>();
        for(Integer i =0; i<acctList.size();i++)
        {
            acctListParent = [Select  name,Id from Account where parentId =: acctList[i].id];
        }
        List<Account> acctFrndAccount = new List<Account>();
        acctFrndAccount =[Select Name,Id from Account where parentId != null];
        System.debug('All Accounts--->'+acctList);
        System.debug('All Childs Accounts-->'+acctListParent);
        System.debug('All frnd Accounts--->'+acctFrndAccount);
    }
}


please mark the best answer if it helps you.

Thanks
Akshay
This was selected as the best answer
Roman Savinuik 9Roman Savinuik 9
SOQL in the loop is not a good idea