You need to sign in to do that
Don't have an account?
GMASJ
List of all parent from given child Account
Hi,
I have a below code which will give lis of child and sub child accounts.
Thanks
Sudhir
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
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
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
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. 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
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
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
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);
}
}
}
}