Based on some of the answers I've browsed I think this might involve 2 queries. I feel like this is probably a pretty common problem so I was hoping someone would have a quick answer or at least be able to point me to the docs that detail how this works. I briefly looked through relational queries but didn't immediately see anything relevant to this kind of bubbling so I am posing the question here.
1 Answer
There's no direct way of fetching a Parent Id from the Account hierarchy using SOQL. However, if you want to use some Apex, you can do it using the below snippet available in the Inline Account Hierarchy package, where currAcctId
is the Child Account for which you need to find the top most Parent Account.
while (!isTopLevelAccount) {
acct = [select a.Id, a.ParentId From Account a where a.Id = :currAcctId limit 1];
if (acct.ParentID != null) {
currAcctId = acct.ParentID;
} else {
isTopLevelAccount = true;
}
}
return currAcctId;