7

The question How to detect the active app in salesforce App menu Programatically using API shows how to determine the current app from the API/Apex, but as a follow-up, how can I determine which tab is currently selected?

describeTabs returns a describeTabSetResult which has a selected attribute indicated whether it is the current app. There's also a tabs parameter listing the tabs inside this app - but I don't see a way to determine which tab is associated with the page currently being displayed.

1 Answer 1

1

One way could be to sniff at the current page's URL and realize that the Id passed is the tab's id (you can use it to access the Custom Tab's detail page in setup).

You could then compare it with tab's URL from the describeTabs() call.

enter image description here

Ctrl:

public class Hello
{
    public String currentApp {get;private set;}
    public String allTabsDebug {get; private set;}
    public String currentTab {get; private set;}
    public String currentTabDebug {get; private set;}

    public Hello(){
        String tabId = ApexPages.currentPage().getParameters().get('sfdc.tabName');

        for(Schema.DescribeTabSetResult dtsr : Schema.describeTabs()) {
            if(dtsr.isSelected()) {
                currentApp = dtsr.label;
                allTabsDebug = JSON.serializePretty(dtsr.tabs);
                if(String.isNotBlank(tabId)){
                    for(Schema.DescribeTabResult dtr : dtsr.tabs){
                        if(String.isNotBlank(dtr.url) && dtr.url.contains(tabId)){
                            currentTab = dtr.label;
                            currentTabDebug = JSON.serializePretty(dtr);
                            break;
                        }
                    }
                }
                break;   
            }           
        }

    }
}

Page

<apex:page controller="Hello">
     <apex:pageBlock title="Input">
         <apex:pageBlockSection title="Input" columns="1">
             <apex:outputText label="URL" value="{!$CurrentPage.url}" />
             <apex:outputText label="sfdc.tabName param" value="{!$CurrentPage.parameters.sfdc.tabName}" />
         </apex:pageBlockSection>
         <apex:pageBlockSection title="Output" columns="1">
             <apex:outputText label="Selected App" value="{!currentApp}" />
             <apex:outputText label="Selected Tab?" value="{!currentTab}" />
             <apex:outputText label="Selected Tab's Debug" value="{!currentTabDebug}" />
         </apex:pageBlockSection>
     </apex:pageBlock>     

    <apex:pageBlock title="Debug (all tabs in that app)">
        <pre>{!allTabsDebug}</pre>
    </apex:pageBlock>
</apex:page>

Not sure about it to be honest, looks bit fragile. But probably will be better solution than doing some CSS sniffing which tab is highlighted and sending it back to Apex with <apex:page action> :)

3
  • Thanks for the answer, but I agree it's fragile. SFDC only appends the sfdc.tabName parameter when you click on a custom tab -- if you click on a standard tab, no parameter appended. Also, if you get to the related page by another means (PageReference to Page.MyPageName for example), no parameter appears.
    – Benj
    Commented May 14, 2014 at 13:32
  • I like the CSS sniffing concept, BUT what I'm actually exploring doing is building my own navigation in place of the tabs -- so the tabs would not be displayed at all!
    – Benj
    Commented May 14, 2014 at 13:33
  • I completely agree with 1st comment :) I'm not sure I get it what you're trying to achieve. If you'll have your own tabbed navigation (with apex:tabPanel or jQuery UI for example) - you should be able to track it yourself? slapping onclick actionSupport/actionFunction on the relevant piece of HTML with apex:param for good measure?
    – eyescream
    Commented May 14, 2014 at 21:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .