Skip to main content

How to get sObject Tab Icon dynamically in Salesforce Apex Class ?

 How to get sObject Tab Icon dynamically in Salesforce Apex Class ?



In this blog we will se how we can get sObjects TabIcon Name dynamically.

Implementation and Use Case:
Use Case:-
  • If we need to select a sObject from a given list of sObjects and then show the Icon for the selected sObject.
  • Pass a sObject name and show the Icon name in Lightning Card.
Implementation:-
Code snippet:-

/**
 * @description       : Set Icon Name/URL
 * @author            : Amit Agarwal
 * @last modified on  : 04-06-2023
 * @last modified by  : Amit Agarwal
**/

public with sharing class SObjectIconController {

    public static String getIconName(String sobjAPIName) {
       
        String iconName;
        List<Schema.DescribeTabSetResult> descTab = Schema.describeTabs();      

        for(Schema.DescribeTabSetResult dts : descTab) {
            List<Schema.DescribeTabResult> tabs = dts.getTabs();        
            for(Schema.DescribeTabResult tab : tabs) {
                if(tab.getSobjectName() == sobjAPIName) {
                    List<Schema.DescribeIconResult> icons = tab.getIcons();                
                    for(Schema.DescribeIconResult icon : icons) {    
                        if(icon.getContentType() == 'image/svg+xml') {
                            iconName = icon.getUrl();
                            break;
                        }
                    }                  
                }          
            }          
        }
       
        if (iconName != null && iconName.contains('custom')) {
            Integer l1 = iconName.indexOf('/custom/') + '/custom/'.length();
            Integer l2 = iconName.length() - 4;
            iconName = 'custom:' + iconName.substring(l1, l2);
        }
       
        if (iconName != null && iconName.contains('standard')) {
            Integer l1 = iconName.indexOf('/standard/') + '/standard/'.length();
            Integer l2 = iconName.length() - 4;
            iconName = 'standard:' + iconName.substring(l1, l2);      
        }
        return iconName;    
    }
}


Now here we have used methods from Schema class. We have used DescribeTabSetResult, DescribeIconResult and DescribeTabResult.

You can learn more about these methods from Salesforce Documents.

Resources: Salesforce Documents

Git resource by Shruti Kulkarni.

Thank you all! 

#Keep Learning #Keep Sharing :) 

Recent blogs-

Comments