We need to show sObject tab icon in listbox or lookup for showing records. An icon will help us identify the type of data easily. We can show tab icons using LWC’s lightning-icon and AURA’s lightning:icon element.
Use Case:
I was preparing a custom lookup element to show data as a picklist. Based on the passed object name I was showing data in custom lookup. Along with data, we need to show the respective sObject icon also based on the passed object name in the custom picklist.
Solution:
We have metadata describing classes Schema.DescribeTabResult, Schema.DescribeTabSetResult and Schema.DescribeIconResult which will help us in getting icon detail.
public class sObjectSelector {
@AuraEnabled
public static String getIconName(String sObjectName){
String u;
List<Schema.DescribeTabSetResult> tabSetDesc = Schema.describeTabs();
List<Schema.DescribeTabResult> tabDesc = new List<Schema.DescribeTabResult>();
List<Schema.DescribeIconResult> iconDesc = new List<Schema.DescribeIconResult>();
for(Schema.DescribeTabSetResult tsr : tabSetDesc) { tabDesc.addAll(tsr.getTabs()); }
for(Schema.DescribeTabResult tr : tabDesc) {
if( sObjectName == tr.getSobjectName() ) {
if( tr.isCustom() == true ) {
iconDesc.addAll(tr.getIcons());
} else {
u = 'standard:' + sObjectName.toLowerCase();
}
}
}
for (Schema.DescribeIconResult ir : iconDesc) {
if (ir.getContentType() == 'image/svg+xml'){
u = 'custom:' + ir.getUrl().substringBetween('custom/','.svg').substringBefore('_');
break;
}
}
return u;
}
}
Show Icon in Lightning Web Component
Test Page:
References:
Related Post
What are Salesforce’s Big Objects?
Generic Notification Component in LWC
Reusable Custom Calendar LWC using FullCalendar Js Library
Custom Toast with custom duration In LWC
Generate and Create Signature in LWC