I have created a detail page button "Edit" in Account object. I want to show the button on account record if the account is "X". and to hide for other accounts. is it possible to hide the button on condition?
[SalesForce] Hide/Show detail page button based on condition
Related Solutions
Changing a variable's value in javascript won't update it in your APEX controller and also won't rerender the commandbutton.
You have 2 options:
- go javascript. Arrange the button visibility by css styling
- go visualforce. Arrange the button visibility by visualforce
I'll give an example of the Visualforce way here, as we're on the salesforce boards :-)
When the checkbox is checked, you want to do 2 things: update the visibleSubmit
boolean and rerender the commandbutton
so it will be shown (or not).
In order to do that, the first step is to associate the visibleSubmit variable directly with your checkbox:
<apex:inputCheckbox id="tncs" required="true" value="{!visibleSubmit}"/>
(Not sure what the partnerAgreement
variable is that is currently in the checkbox? If that's a boolean as well, you might use that instead of visibleSubmit to decide whether or not to render the button. But then you also have to use 'partnerAgreement' in the 'rendered' option of the commandButton.)
Secondly, you need to rerender the commandbutton when the checkbox changes. The way to do that is using an actionsupport. The actionsupport can be directly attached to the checkbox. Also you need to wrap the commandButton in a panel, so that you can easily rerender the panel:
<apex:inputCheckbox id="tncs" required="true" value="{!visibleSubmit}">
<apex:actionSupport event="onchange" rerender="theButtonPanel" />
</apex:inputCheckbox
<apex:outputPanel id="theButtonPanel>
<apex:commandButton styleClass="btn" value="Submit" id="recordSubmit" rendered="{!visibleSubmit}" action="{!insertRecord}"/>
</apex:outputPanel>
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.
Best Answer
You can't hide button but you can add validation on button and perform validation based on your requirement for Onclick Javascript Custom button.
Ex: