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

Popular posts from this blog

How to update Field Level Security in bulk ?

Assign Field Level Security(FLS) in Salesforce Demo:- In this blog we will see the easy way to update Field Level Security(FLS) for Permission sets/Profiles in bulk. I have created a tool to make the Admins job a little easier. Before jumping to tool let us see the different standard approach we follow to update FLS in Salesforce.                     As an admin we usually update FLS from salesforce setup/UI. If we are working on a new Application with many Permission sets and Objects with 100+ fields it can be very time consuming task. Using this tool we can assign FLS in just few mins. Let us take an example. Suppose we have a new application and we have created 20 Permission sets and 8 Profiles . Now there are 10 Objects in all and in each object we have 15 fields for which we need to update FLS for above Permission sets and Profiles. Let us first see the different approach to assign FLS in Salesforce. We have two ways in...

Nested Accordion in Salesforce LWC

Are you new to LWC ? Are you wondering how to loop in and make a nested accordion in LWC.? No worries. You are are at the right place. Today in this blog we will go through this simple requirement. We will see how we can loop and use lightning-accordion in our code. Use Case: Use case is very Simple . We need to Print all Contacts which are related to a particular account . We have done this several times using different methods. But in this blog we will see how we can use map to get Account Name as Key and the related contacts as the Value. It will have learn following in this blog: How to create Key map to pull all the Account Records which has atleast one Contact in key:value fashion. How to use Wire service to pull data. How to use Lightning Accordion. Let us jump into code now. Solution : Apex Controller In this Apex Class we are fetching all Contact records and the Account Name to which it is associated. We are then creating a map of Type Map<String, List...