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
GMASJGMASJ 

List of all parent from given child Account

Hi, 

 I have a below code which will give lis of child and sub child accounts. 
 
Id accountId = '0013400001R8KXJ';

Account[] allChildren = new Account[] {};

Set<Id> parentIds = new Set<Id>{accountId};

Account[] children;

do {

    children = [select Id, Name from Account where ParentId in :parentIds];

    allChildren.addAll(children);

    parentIds.clear();

    for (Account child : children) 

      parentIds.add(child.Id);

} while (children.size() > 0);

list<Account> Act = [select id, name from account where id in :allChildren];

 for(Account A : Act){

 system.debug(a.name);       

}
Please suggest me how to write a code to get parent and grand parent list of accounts using apex. 

Thanks
Sudhir
 
Shiva RajendranShiva Rajendran
Hi Sudhir,

 
Id accountId = '0013400001R8KXJ';

Account[] allParent = new Account[] {};
Account[] allAccount = new Account[] {}; 

Set<Id> childIds = new Set<Id>{accountId};
Map<Id,Id> parentAssociatedMap= new Map<Id,Id>();
Map<Id,Id> grantParentAssociatedMap= new Map<Id,Id>();
Set<Id> allParentIdSet=new Set<Id>();
Set<Id> allgrantParentIdSet=new Set<Id>();

List<Account> parent =new List<Account>();


    allAccount = [select Id,ParentId,Name from Account where Id in :childIds];

   for(Account acc:allAccount)
   {
          parentAssociatedMap.put(acc.Id,acc.ParentId);
          allParentIdSet.add(acc.ParentId);
parent 
   }

GMASJGMASJ

Hi Shiva, 

   Thanks for you help in writting the code I checked you code 

   I need grand parent till the top parent have partent id = null 

   Example 
  •     Parent 1
    • Parent 2
      • Parent 3
      • Parent 4
        • Parent 5

In Above example if I search for Parent 5 it should display Parent 4 - Parent 2- Parent 1 till top level parent 1 Can you please suggest me to modify the code. 

Thanks
Sudhir
   
Shiva RajendranShiva Rajendran
  Hi Sudhir,

 Basically one child object can't have more than one parentObject in salesforce. ( One parent can have multiple child its not vice versa).
So you can directly access it using ParentId.
But since you asked code for it..The below code satisfies your requirement.
Id accountId = '0013400001R8KXJ';

Account[] allParent = new Account[] {};
Account[] allAccount = new Account[] {}; 

Set<Id> childIds = new Set<Id>{accountId};

Map<Id,Id> parentAssociatedMap= new Map<Id,Id>();

Map<Id,Account> parentIdAndRecordMap = new Map<Id,Account>();
Map<Id,Id> parentRecordAssociatedMap = new Map<Id,Account>();


Map<Id,Id> grantParentIntermediateAssociatedMap= new Map<Id,Id>(); //its like an junctionObj

Map<Id,Id> grantParentAssociatedMap= new Map<Id,Id>();
Set<Id> allParentIdSet=new Set<Id>();
Set<Id> allgrantParentIdSet=new Set<Id>();



    allAccount = [select Id,ParentId,Name from Account where Id in :childIds];

   for(Account acc: allAccount)
   {
          parentAssociatedMap.put(acc.Id,acc.ParentId);
          allParentIdSet.add(acc.ParentId);
          
   }
allParent =[select Id,ParentId,Name from Account where Id in :allParentIdSet];

 for(Account acc: allParent ) 
{ 
  grantParentAssociatedMap.put(acc.Id,acc.ParentId);
/*
  if needed account record instead of id
grantParentIntermediateAssociatedMap.put(acc.Id,acc.ParentId);
*/
  allgrantParentIdSet.add(acc.ParentId);
 }

/*
// if needed to have the parentrecord instead of parent id ..follow the below code
 
parentIdAndRecordMap = new Map<Id, Account>(allParent);

for(Id id: parentAssociatedMap.keyset())
{ 
parentIdAndRecordMap.put(id,parentIdAndRecordMap.get(parentAssociatedMap.get(id));
//parentAssociatedMap get(id) gives the parentId,parentIdAndRecordMap.get(id) gives the record
}
*/
Hope this helps,
Let me know if you need further help. This is not verified code,i wrote on the fly , so may have flaws.

Thanks and Regards,
Shiva RV
Shiva RajendranShiva Rajendran
Hi Sudhir,
Sorry for the lag.The second answer was the one i intented to sent first.
okay .Will it be present inside a trigger or not? i need to know if your requirement is for a list of child records or only for a one or two records?
If only few records ,its easily else we must follow the best approach. May need time to answer your question

Thanks and Regards,
Shiva RV
GMASJGMASJ
Hi Siva, 

 Acually the entire logic will be places inside a contact trigger calling a helper class. I am first trying inside a ananomious block to see how it works. 

Please take your time also you can try this in your org environment I want to try this first by passing a account id. 

If a sub child account id is passed I need to see all the connect parent till the top level parent where its parent id = null thats the requirement. 

I am going through your code let me see if i can tune appericaite your help. 

Thanks
Sudhir









 
Sajid Hussain 15Sajid Hussain 15
Hi, i know its late answer but i have seen this question while i was doing search. I have made code for N number of parent. Any one can achieve it with recursive. I just write parents in console. You can play with global values. But its taking till end parent of child.

public class RecursiveCls {
    public void all()
    {
        account[] allAccount = [select Id,ParentId,Name from Account order by id desc limit 5];
        for(account act:allAccount)
        {
            if (act.ParentId != null)
            {
                system.debug('Parent of '+act.Name);
                recursive(act.ParentId);
            }
            else 
            {
                system.debug('end = '+act.Name);         
            }
        }
    }
    public void recursive(string accountId)
    {
        account[] allAccount = [select Id,ParentId,Name from Account where Id =:accountId];
        for(account act:allAccount)
        {
            if (act.ParentId != null)
            {
                system.debug('--1> '+act.Name);
                recursive(act.ParentId);
            }
            else 
            {
                system.debug('---2> '+act.Name);         
            }
        }
    }
}