Skip to content

Instantly share code, notes, and snippets.

@Sunil02kumar
Last active May 2, 2023 08:24
Show Gist options
  • Save Sunil02kumar/1d7d8d10304f8635db9c9cebaecade69 to your computer and use it in GitHub Desktop.
Save Sunil02kumar/1d7d8d10304f8635db9c9cebaecade69 to your computer and use it in GitHub Desktop.
Account Hierarchy Using Tree Grid in LWC
<template>
<div>
<lightning-card title="Account Hierarcy">
<lightning-tree-grid
columns={gridColumns}
data={gridData}
key-field="Id"
hide-checkbox-column="true"
expanded-rows={currentExpandedRows}>
</lightning-tree-grid>
</lightning-card>
</div>
</template>
import { LightningElement,wire,api } from 'lwc';
import fetchAccounts from '@salesforce/apex/skAccountHierarchyCmpController.findAllHierarchyAccounts';
const COLLS = [
{
type: 'url',
fieldName: 'AccountURL',
label: 'Account Name',
typeAttributes: {
label: { fieldName: 'accountName' },
target: '_self'
}
},
{
type: 'text',
fieldName: 'Industry',
label: 'Industry'
},
{
type: 'text',
fieldName: 'Type',
label: 'Type'
}
];
export default class SK_AccountHierarchyCmp extends LightningElement {
gridColumns = COLLS;
gridData = [];
roles = {};
currentExpandedRows=[];
@api recordId;
@api objectApiName;
@wire(fetchAccounts, { recordId: '$recordId'})
AllAccountInfo({ error, data }) {
if (error) {
console.error("error loading accounts", error);
} else if (data) {
//console.log('*****dat from apex:'+JSON.stringify(data));
console.log('**ObjectAPI Name:'+this.objectApiName+'***current account Id:'+this.recordId);
var finaldata=[];
var expandedRowInfo=[];
for ( var i = 0; i < data.length; i++ ) {
if(data[i].ChildAccounts){
expandedRowInfo.push(data[i].Id);
this.roles[data[i].Id] = {
accountName: data[i].Name ,
Id: data[i].Id,
AccountURL:'/'+data[i].Id,
Type:data[i].Type?data[i].Type:'',
Industry:data[i].Industry?data[i].Industry:'',
_children: []
};
}else{
this.roles[data[i].Id] = {
accountName: data[i].Name ,
Id: data[i].Id,
AccountURL:'/'+data[i].Id,
Type:data[i].Type?data[i].Type:'',
Industry:data[i].Industry?data[i].Industry:''
};
}
}
for ( var i = 0; i < data.length; i++ ) {
if(data[i].ParentId){
if(this.roles[data[i].ParentId]){
this.roles[data[i].ParentId]._children.push(this.roles[data[i].Id]);
}
}
}
//console.log('***after adding childrens :'+JSON.stringify(this.roles));
for ( var i = 0; i < data.length; i++ ) {
if(data[i].ParentId){}
else{
finaldata.push(this.roles[data[i].Id]);
}
}
console.log('***finaldata :'+JSON.stringify(finaldata));
this.gridData=finaldata;
this.currentExpandedRows=expandedRowInfo;
console.log('***currentExpandedRows 2:'+JSON.stringify(this.currentExpandedRows));
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>55.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordPage</target>
<target>lightning__AppPage</target>
<target>lightning__HomePage</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__RecordPage">
<property
name="recordId"
type="String"
label="Record Id"
description="Pass the page's record id to the component variable"
default="{!recordId}" />
<property
name="objectApiName"
type="String"
label="Object Name"
description="Pass the page's object name to the component variable"
default="{!objectApiName}" />
</targetConfig>
</targetConfigs>
</LightningComponentBundle>
public with sharing class skAccountHierarchyCmpController {
@AuraEnabled(cacheable=true)
public static list<Account> findAllHierarchyAccounts(string recordId){
list<Account> allAccountList=new List<Account>();
string ultimateAccountId;
for(Account acc:[select id,Ultimate_Account_Id__c from Account where Id=:recordId]){
ultimateAccountId=acc.Ultimate_Account_Id__c;
}
if(string.isNotBlank(ultimateAccountId)){
for(Account acc:[select id,(select id from ChildAccounts),Name,ParentId,Type,Parent.Name,Industry
from Account where Ultimate_Account_Id__c=:ultimateAccountId order by parentId NULLS FIRST]){
allAccountList.add(acc);
}
}
system.debug('***allAccountList size:'+allAccountList);
return allAccountList;
}
}
@Smartronics99
Copy link

Smartronics99 commented May 2, 2023

Hello Sunil,
Created Ultimate_Account_Id__c and implemented this shared code. However, I am getting only one level, i.e., Parent.

image

image

could you please suggest , what am I missing here ? How to get remaining child and grandchild

Thank you very much !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment