I have a VF page with a requirement to show a commandbutton only for a specific tab. Can I do this with a {!TabInFocus} method? I want to show the 'Add Contact' button only when the Contacts tab is currently selected.

VF Page

<apex:page controller="AccountPricingRanges" sidebar="false" action="{!onloadmethod}" showheader="false">
    <apex:form >
        <apex:pageBlock title="Business Profile for {!acc.Name}">
        <apex:outputLink value="/{!acc.id}"><p>Back to {!acc.name}</p></apex:outputLink>
        <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
                <apex:commandbutton value="Add Contact" action="{!addContact}" immediate="true"/>
                <apex:commandbutton value="Add Timeslot" action="{!addTimeslot}" immediate="true"/>
            <apex:tabPanel value="{!TabInFocus}">    

                <apex:tab label="Contacts" labelWidth="180" styleClass="label">
                    <apex:pageBlockTable value="{!contacts}" var="con"> <apex:variable value="{!0}" var="cnt"/>

                        <apex:column headervalue="Action">
                            <apex:commandlink value="Remove" action="{!removeContact}" immediate="true">
                                <!-- Pass the row number to the controller so that we know which row to remove -->
                                <apex:param name="index" value="{!cnt}"/>
                            <apex:variable var="cnt" value="{!cnt+1}"/>              

                        <apex:column headerValue="First Name">
                            <apex:inputField value="{!con.FirstName}"/>

                        <apex:column headerValue="Last Name">
                            <apex:inputField value="{!con.LastName}"/>

                        <apex:column headerValue="Email">
                            <apex:inputField value="{!con.email}"/>

                        <apex:column headerValue="Phone">
                            <apex:inputField value="{!con.phone}"/>



public with sharing class AccountPricingRanges {

    public List<Contact> contacts {get;set;}
    Id accountid = ApexPages.CurrentPage().GetParameters().Get('id');

    public void onloadmethod(){
        contacts = [Select FirstName, LastName, Email, Phone
                 FROM Contact
                 WHERE AccountId =:accountid
                 ORDER BY FirstName ASC];
        timeslots = [Select Name, Day_of_Week__c, End_Time__c, Start_Time__c
                     FROM Franchise_Timeslot__c
                     WHERE Franchisee__c =:accountid
                     ORDER BY Day_of_Week__c ASC];

    String tabInFocus = System.currentPageReference().getParameters().get('tab');
    public String getTabInFocus() {
        return tabInFocus;
    public void setTabInFocus( String s ) {
        this.tabInFocus = s;

    public List<Contact> getcontacts() {
        contacts = [Select FirstName, LastName, Email, Phone, Contact_Type__c
                 FROM Contact
                 WHERE AccountId =:accountid
                 ORDER BY FirstName ASC];
        return contacts;
  • 1
    You are much more likely to get a useful answer if you trim your code and markup down to just what is necessary to illustrate the issue. Your post contains a massive amount of both for a question like this. – Adrian Larson Oct 18 '17 at 20:39

You could use the ontabenter="" and ontableave attributes on apex:tab, to register the current tab and in your button, you can check if the tab is selected.

<apex:actionFunction action="{!doAnything}" name="tabSelected" rerender="buttons">
    <apex:param assignto="{!contactTabSelected}" name="selected" value=""/>

<apex:pageBlockButtons id="buttons">
    <apex:commandbutton value="Add Contact" action="{!addContact}" immediate="true"

<apex:tab label="Contacts" labelWidth="180" styleClass="label"


public Boolean contactTabSelected { get; set; }

public void doAnything() {

right now I'm not sure if you even need the doAnything method, please try it and give feedback. All I know is you can skip the action, but I'm not sure if params will be passed .

| improve this answer | |
  • This is what I found regarding you inFocus question: force201.wordpress.com/2012/05/31/… – Basti Oct 18 '17 at 21:16
  • ontableave might cause some unexpected behavioir: The JavaScript invoked if the ontableave event occurs--that is, if a component outside the tab becomes in focus. If it doesn't work, you could just only use ontabenter on every tab, to keep track of the selected tab and pass their name to the action method. – Basti Oct 19 '17 at 8:04
  • Thanks for the comment. I have tried this and it did not work. The 'Add Contacts' tab does not display at all. – Sebastian Oct 19 '17 at 14:37

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.