In this blog, I am going share a approach through which you can display account hierarchy using lightning tree grid. You can place this lightning component on account record page and it will detect the current account record Id and will display the complete hierarchy.
I am utilising formula field called "Ultimate Account id" to avoid multiple SOQL in apex to get list of all account in the hierarchy. This component can display all account which are up to 10 level deep in hierarchy.
Please refer below URL to get more idea about Ultimate Account Id formula field.
How to get list of all accounts present in account hierarchy in Salesforce
In this LWC component, I am just passing the account Id to apex method and apex method returns all the account list which are present in hierarchy. In order to display account in tree grid, I am creating data in js which will be accepted by tree grid. So in this approach, manipulation on apex side is very simple.
Note: Create Ultimate Account Id formula field on account first in order to implement below code.
Complete Code:
Hope this will help!!!