Winter'18 Salesforce have added a new lightning component called lightning:tree, using this we can display the hierarchy of Salesforce.
In this component is going to be great help to all the devs, when they want achieve the accordion looking hierarchy displaying component. This component support only version 41.0 and above
So in this post we have a small component which displays the account hierarchy upto 3 levels, I am not further on achieving the hierarchy of Account using Apex, so I did what I can using just a single SOQL.
Some important links to learn more about Winter'18 Release
Thanks you
In this component is going to be great help to all the devs, when they want achieve the accordion looking hierarchy displaying component. This component support only version 41.0 and above
Here is the working demo
So in this post we have a small component which displays the account hierarchy upto 3 levels, I am not further on achieving the hierarchy of Account using Apex, so I did what I can using just a single SOQL.
Here is the component code
<aura:component controller="LightningTreeApexController" implements="flexipage:availableForRecordHome,force:lightningQuickActionWithoutHeader,force:hasRecordId"> >
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<aura:attribute name="items" type="Object"/>
<aura:attribute name="recordId" type="String"/>
<lightning:tree items="{! v.items }" header="Account Hierarchy" onselect="{!c.handleSelect}"/>
</aura:component>
Here Lightning component Controller code
({
doInit: function (cmp, event, helper) {
helper.apexMethod(cmp);
},
handleSelect: function (cmp, event, helper) {
//return name of selected tree item
var myName = event.getParam('name');
alert("You selected: " + myName);
}
})
Here is the helper code
({
apexMethod : function(cmp) {
var action = cmp.get("c.getAccountHierarchy");
action.setParams({ accountId : cmp.get("v.recordId") });
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
cmp.set( "v.items", response.getReturnValue());
}
});
$A.enqueueAction(action);
}
})
Here is Apex Controller
public class LightningTreeApexController {
@AuraEnabled
public static List<items> getAccountHierarchy(Id accountId) {
//Wrapper instance
List<items> finalWrp = new List<items>();
//Going upto 2 level only as per SOQL limit
for(Account acc : [ Select Id, Name, Type, ParentId, Parent.Name, Parent.Type, Parent.ParentId, Parent.Parent.Name, Parent.Parent.Type From Account Where Id =: accountId]) {
//populating wrapper
List<items> trP1 = new List<items>{new items(acc.Type, acc.Name, false, null)};
List<items> trP2 = new List<items>{new items(acc.Parent.Type, acc.Parent.Name, false, trP1)};
finalWrp.add(new items(acc.Parent.Parent.Type, acc.Parent.Parent.Name, false, trP2));
}
System.debug('finalWrp:::' + finalWrp);
// return wrapper
return finalWrp;
}
}
Here is wrapper class used in Apex controller
public class items {
@AuraEnabled
public string label { get; set; }
@AuraEnabled
public string name { get; set; }
@AuraEnabled
public Boolean expanded { get; set; }
@AuraEnabled
public List<items> items { get; set; }
public items( String name, String label, Boolean expanded, List<items> items) {
this.label = label;
this.name = name;
this.expanded = expanded;
this.items = items;
}
}
Some important links to learn more about Winter'18 Release
Salesforce Documentation
Lightning Design System : Lightning Tree
Salesforce Release Notes Winter'18
Thanks you
This comment has been removed by the author.
ReplyDeleteWhen you have the
ReplyDeleteimplements="...,force:hasRecordId"
is the
<aura:attribute name="recordId" type="String"/>
needed ?
Then how you will use the recordId of Account with using recordId attribute?, I am using attribute recordId to get the account Id, if you have any other way of doing that, then you can try that one as well.
DeleteI think the aura framework does it for you. If you put the ltg component on an Account page layout then it gets the Account record id automatically because of the 'force:hasRecordId'.
DeleteI took your example component and removed the recordId line. It works just fine without it.
Awesome, thanks for letting me know.
DeleteFrom Salesforce documentation:
ReplyDelete"If your component implements force:hasRecordId, you don’t need to add a recordId attribute to the component yourself. If you do add it, don’t change the access level or type of the attribute or the component will cause a runtime error."
Hello! Great sample. Instead of name, how do I retrieve the label? Using event.getParam('label') won't work.
ReplyDeleteThank ou
Hi,
ReplyDeleteI am using the lightning:tree on a custom lightning component to show the account hierarchy. Everything is working fine except the expand/collapse icons are not visible.
Any help would be appreciated.
Thanks,
RR
Just recheck you have everything in version 41 or more.
Deletethanks
Hi.Thats a very helpful article.
ReplyDeleteBut Is there any way that we can add more fields on the tree along with account name.
I would like to show more field onthe tree display instead of just name. Please guide in that regards.
Thanks
Swetha
Awesome post.. Good explanation. Helpful one!
ReplyDeleteSalesforce Lightning Training
ReplyDeleteVery Impressive Salesforce Lightning tutorial. The content seems to be pretty exhaustive and excellent and will definitely help in learning Salesforce Lightning. I'm also a learner taken up Salesforce training and I think your content has cleared some concepts of mine. While browsing for Salesforce Lightning tutorials on YouTube i found this fantastic video on Salesforce Lightning. Do check it out if you are interested to know more.:-https://www.youtube.com/watch?v=SrmlBPnHqO0&t=19s
ReplyDeleteVery Impressive Salesforce Lightning tutorial. The content seems to be pretty exhaustive and excellent and will definitely help in learning Salesforce Lightning. I'm also a learner taken up Salesforce training and I think your content has cleared some concepts of mine. While browsing for Salesforce Lightning tutorials on YouTube i found this fantastic video on Salesforce.Do check it out if you are interested to know more.:-https://www.youtube.com/watch?v=MOH8Zr7hm5U
hello i have asimilar requirment but it extened to n levels a object with child and then child and son on can you help me the best design
ReplyDeleteHi,
ReplyDeleteIs there a way to open the link in new window or tab??
TIA!
I would like to know more about these suggestive points. Give me little more brief and I think they can help me. Here the mentioned points are very useful and I am definitely going to adopt in my life.
ReplyDeletebelastingconsulent amsterdam
Most of educational information over different kinds of blogs do not such supportive as supportive all the points of this blog. You need not to find any other platform to verify the data stated here.
ReplyDeletebioresonantie groningen
Constructor not defined: [items].(String, Boolean, List)
ReplyDeleteerror is occuring in Apex class.
nice
ReplyDeletecodey expandable is not coming for me ,although i used the same code ...
ReplyDeleteplz reply...the codey expandable is not coming under Account Hierarchy
ReplyDeleteGet huge discount on Home and Kitchen Appliances,Split and Window Air Conditioner, Mobiles & Laptops online , Television, Speakers & more electronics at best price.
ReplyDeletesamsung washing machine
I was eager to find this page. I needed to thank you for ones time for this especially awesome read!! I certainly truly preferred all aspects of it and I likewise have you book-set apart to look at new data in your blog.
ReplyDeletebuy Medicare Insurance
İsmetpaşa
ReplyDeleteKnidos
Sanayi
İhsaniye
Bostanlı
N55V
Diyarbakır
ReplyDeleteSamsun
Antep
Kırşehir
Konya
SKHW2
van
ReplyDeletekastamonu
elazığ
tokat
sakarya
OSTER8
A6018
ReplyDeleteTokat En İyi Görüntülü Sohbet Uygulaması
bedava sohbet chat odaları
Burdur Rastgele Sohbet Uygulaması
manisa en iyi görüntülü sohbet uygulaması
uşak mobil sohbet siteleri
adıyaman muhabbet sohbet
canlı sohbet
kocaeli canlı sohbet odası
Gümüşhane Sohbet Chat
705EA
ReplyDeletemaraş canlı sohbet et
uşak ücretsiz görüntülü sohbet
ığdır canlı ücretsiz sohbet
hakkari bedava sohbet siteleri
kızlarla rastgele sohbet
çorum rastgele görüntülü sohbet
mobil sohbet et
sesli sohbet sitesi
Antep Canlı Ücretsiz Sohbet
C80F2
ReplyDeleteYalıhüyük
Pülmür
Kofçaz
Yayladere
Mazgirt
Üsküdar
Nazimiye
Hemsin
İnhisar
FF73A
ReplyDeletefiltre kağıdı
binance
bingx
https://toptansatinal.com/
kucoin
telegram kripto para kanalları
canlı sohbet siteleri
toptan mum
gate io
C4E73
ReplyDeletegörüntülü+şov+ücretli
11BD3
ReplyDeletegörüntülü ücretli şov