I have a custom button created whose logic which invokes a visualforce page. I have overridden the view button with a visualforce page that just displays the detail page of salesforce. The custom button is added to the detail page of the record.

View Page just has the below:

<apex:page standardController="object__c" extensions="controller">

 <apex:pageMessages />
 <apex:detail subject="{! object__c.id}"/>

Controller logic:

public class controller{

private static final Map<String, ApexPages.Severity> severityMap = new Map<String, ApexPages.Severity>{'INFO' => ApexPages.Severity.INFO, 'ERROR' => ApexPages.Severity.ERROR, 'CONFIRM' => ApexPages.Severity.CONFIRM };
   public object__c recVar{get;set;}
   public String recordId;
    public VFPage(ApexPages.StandardController controller) {
            recVar= (object__c) controller.getRecord();
            recordId = String.valueOf(sr.Id).substring(0, 15);
            String pageMessage = ApexPages.currentPage().getParameters().get('pageMessage');
            boolean doNotDisplay;
            String referrer = ApexPages.currentPage().getHeaders().get('REFERER');
            system.debug('############ the value ################' + referrer );
            if(referrer.contains(recordId + '/e')){
            System.debug('Do not display boolean %%%%%%%' + doNotDisplay);
          ApexPages.Severity whatSeverity;
          if(ApexPages.currentPage().getParameters().get('severity') !=NULL){
            whatSeverity = severityMap.get(ApexPages.currentPage().getParameters().get('severity'));
            System.debug('Severity %%%%%%%' + whatSeverity);
        if (pageMessage != null && doNotDisplay) {
            ApexPages.addmessage(new ApexPages.Message(whatSeverity , pageMessage));
            System.debug('pageMessage  Error **' + pageMessage );



The custom button has the reference to the below VF page:

<apex:page standardController="object__c" extensions="acontroller" action="{!makecallout}">

The challenge here:

the challenge here is I want to disable the custom button based on a formula field value in the record. If the formula field return true I want to show the button else if it is false I do not want to show the button. How can I show and hide the button in the above VF page?

  • can you add the custom button code in the question May 15, 2017 at 19:07
  • The 2nd visualforce page is behind the custom button. It calls the makecallout function.
    – SfdcBat
    May 15, 2017 at 19:08
  • pls add the custom button code in the 1st visualforce page in the question May 15, 2017 at 19:09
  • Do you want to know the what is in the controller of the 1st VF page? The custom button code is part of 2nd VF page's controller.
    – SfdcBat
    May 15, 2017 at 19:12

2 Answers 2


You should use rendered and disabled attribute of apex:commandButton for displaying or disabling based on formula condition.

Sample code looks like this:

<apex:commandButton action="{!callVFP}" 

Refer apex:commandButton

  • rendered - A Boolean value that specifies whether the component is rendered on the page. If not specified, this value defaults to true.

  • disabled - A Boolean value that specifies whether this button should be displayed in a disabled state. If set to true, the button appears disabled. If not specified, this value defaults to false.

Update based on comments

You cannot manage to show and hide custom button through <apex:detail/> tag.

If that is the situation, then you need to rebuild the page.

Otherwise, create recordtype and separate page layouts( with and w/o custom button) and update the record with proper recordtype.

  • Thanks. In my case I do not use <apex:commandButton> Instead I create a button and put it in detail page layout. Is there a way hide that type of button?
    – SfdcBat
    May 15, 2017 at 19:17
  • yes, possible, pls edit your question with that code May 15, 2017 at 19:18
  • I have added the logic of controller of view page.(1st VF page ) where <apex:detail> tag is present. Please let me know how can I hide the button which is part of standard detail page.
    – SfdcBat
    May 15, 2017 at 19:23
  • pls see my updated answer and I think recordtype options will be good May 15, 2017 at 19:32
  • 1
    make this as default recordtype so that it will not show up during record create. That is also configurable. May 15, 2017 at 22:43

This can be achieved using Jquery as well. Create a formula field which hold the condition when the button should be shown/hidden. Use this field in VF page where you use Jquery to hide the button based on the name attribute of the button & the formula field. Like below:

<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" />
$(document).ready(function() {
if({!formula_field__c == 'true' }){

You must log in to answer this question.

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