[SalesForce] How to hide a custom button in detail page

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}"/>
</apex:page>

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')){
            doNotDisplay=false;
            }else{
            doNotDisplay=true;
            }
            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}">
</apex:page>

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?

Best Answer

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}" 
value="callVisualforce" 
id="theButton" 
rendered="{!condition}" 
disabled="{!condition}"/>

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.

Related Topic