[SalesForce] How to detect the active app in salesforce App menu Programatically using API

I have an appexchange application which is going to be installed in customer orgs. I need to show a sidebar component if the app selected from app menu is my installed package. When I change the app menu from the installed package to something else I need to hide the side bar component. I know this can be done by javascript depending on the DOM structure of salesforce app menu but it is dangerous as Salesforce can change at any time. So please let me know is there any work around to detect the salesforce active app / change in active app.

Best Answer

You can use the describeTabs method to list the current applications and determine if the selected app is the one your looking for. I created a HTML side bar component with an IFRAME containing this page, when i switch applications it updates with accordingly.

enter image description here

enter image description here

<apex:page controller="MyAppController" showHeader="false" sidebar="false">{!message}</apex:page>

The following displays the message accordingly..

public with sharing class MyAppController {

    public String getMessage()
    {
        String appName = 'Sales';

        // Get tab set describes for each app
        List<Schema.DescribeTabSetResult> tabSetDesc = Schema.describeTabs();

        // Iterate through each tab set describe for each app and display the info
        boolean selected = false;
        for(Schema.DescribeTabSetResult tsr : tabSetDesc) {
            if (tsr.getLabel() == appName) {
                selected = tsr.isSelected();
                break;   
            }           
        }       

        // Selected?
        return selected ? 'Sales selected' : 'Sales not selected';
    }
}
Related Topic