7

Assume there is a custom object elfBCQuote__c having an Tab and an Icon assigned during object creation via Setup.

Now I am on a VF-Page and have only the objects name elfBCQuote__c as string. Is there any way to get the correct lightning icon name of that given object?

I need to identify that icon:

enter image description here

I need get something like custom/custom41 or custom:custom41 to dereference the SVG from SLDS in my own VF-comoponents. VF, JS or APEX solutions or even ideas are welcome!

Annex 1

I'm also interested of getting the same inside my lightning-components. Solution via markup, js-controller, apex-controller or otherwise are all welcome!

Annex 2

Actually I was able to do it for an <apex:relatedList> but my solution is such an evil hack, that I'm not going to post it here... But @saleforce: we need this urgently. So this question stays still open. However the result (believe it or not it's a visualforce page) looks like this:

enter image description here

  • 2
    Good question ! I needed the same but didn't find anything :( – Martin Lezer Aug 30 '17 at 15:19
  • @MartinLezer there should be a way to get the objects icons via schema or any other way - but you are right, it seems like it is either well hidden or not existent at all... – Uwe Heim Aug 30 '17 at 15:21
  • 1
    This definitely should be in the DescribeSObjectResult class. You have it for tabs but not objects. Another possible workaround - if you are in 1 org (not a developing a package for many orgs) you can define a table (custom setting) that maps each object with it's related icon – Itai Shmida Aug 31 '17 at 4:54
3

I came across something which could do the trick. But that's a bit ugly...

In fact, you can get the different tabs used in an app and each icon url related to theses tabs.

Finally, by checking if the tab is related to the sobject you're searching for, you're able to return the icon name by removing the other parts of the icon url.

Here's my class:

public class IconsUtil {
    private static final String LIGHTNING_ICON_EXT = '.svg';

    private Map<String, String> iconsNamesBySObjectNames;

    public IconsUtil(){
        this.iconsNamesBySObjectNames = new Map<String, String>();

        //we loop through each application
        for(Schema.DescribeTabSetResult tsr : Schema.describeTabs()) { 
            //that's the current selected application
            if(tsr.isSelected()){
                //we loop through each tab
                for(Schema.DescribeTabResult tabResult : tsr.getTabs()){
                    //there are different icons related to this tab. Some for the vf version, some for Lightning. And also depending on the icon size.
                    for(Schema.DescribeIconResult iconResult : tabResult.getIcons()){
                        String iconUrl = iconResult.getUrl();
                        if(iconUrl.endsWith(LIGHTNING_ICON_EXT)){
                            //Unfortunately, that's the whole url. So we extract the end without the extension
                            this.iconsNamesBySObjectNames.put(tabResult.getSobjectName(), iconUrl.substringAfterLast('/').substringBefore(LIGHTNING_ICON_EXT));
                            break;
                        }
                    }    
                }
                break;
            }
        }
    }

    public String getBySObjectName(String sobjectName){
        return this.iconsNamesBySObjectNames.get(sobjectName);
    }
}

Here's how to use it:

IconsUtil iconsUtil = new IconsUtil();
String iconName = iconsUtil.getBySObjectName('MyCustomObject__c');
0

I tried above solution but is was not working for all the objects. For some custom objects above solution was returning blank values. So I tried using ui-api.

Here is code for that

        HttpRequest req = new HttpRequest();
        req.setEndpoint(orgUrl+'/services/data/v43.0/ui-api/object-info/'+objectName);
        req.setHeader('Content-type','application/json; charset=UTF-8');
        req.setHeader('Accept','application/json');
        req.setMethod('GET');
        req.setHeader('Authorization', 'OAuth ' + sessionId);        
        Http http = new Http();
        HTTPResponse res = http.send(req);
        String response = res.getBody();
        if(res.getStatus() != 'OK'){
            throw new ApplicationException(res.getStatus());
        }
        Map<String,object> apiResponse =(Map<String,Object>)JSON.deserializeUntyped(response);
        Map<String,object> themeInfo = (Map<String,object>)apiResponse.get('themeInfo');
        String objectIcon;
        if(themeInfo != null){
        String iconUrl = (String)themeInfo.get('iconUrl');
        if(String.isNotBlank(iconUrl)){
            List<String> urlList = iconUrl.split('/');
            if(urlList.size() > 2){
                String iconSvg = urlList[urlList.size() - 1];
                String iconName = iconSvg.substringBeforeLast('_');
                objectIcon = urlList[urlList.size() - 2]+ ':' + iconName;
            }
        }
    return objectIcon;

ui-api returns theme info object in which it contains a iconUrl key. We can retrieve iconName from that url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.