+ Start a Discussion
hemanth tanguturi 5hemanth tanguturi 5 

SOQL query for below scenario

Hi,

Scenario:
In the account object we have "parent account" lookup field. User is selecting parent account when creating an account.

Acc A ---> grand parent account
    Acc B ----> parent account
        Acc C----> Account

While creating Account B I have selected Account  A as parent account.
While creating Account C I have selected Account  B as parent account.

a) I want to query all parent account of C(which will inclue parent and grand parent). How can I achieve this?

b) For example account B is selected as parent account for both Acc C and Acc D then, I want a query to pull all child accounts for Account B

Thanks in advance

Hemanth
 
sandeep@Salesforcesandeep@Salesforce
You can try aggregate query for this if your parent id field is Parent_Account_c

SELECT Parent_Account_c, count(Id)  FROM Account GROUP BY Parent_Account_c

using this you can save lot of coding struggle. 

Thanks
Sandeep Singhal