Home SalesforceApex sObject Tab icon in Salesforce Apex

sObject Tab icon in Salesforce Apex

by Dhanik Lal Sahni

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:

Get sobject tab icon

Related Post

What are Salesforce Big Objects?

You may also like

Leave a Comment

Top 10 Enterprise Integration Use Cases Top 10 SOQL Best Practices Top 10 Salesforce Security Best Practices Salesforce Spring ’23 Enhancement in Lightning Components Salesforce Spring ’23 Enhancement in Apex