When a Salesforce Visualforce page performs an AJAX (Asynchronous JavaScript and XML) request — for example, when a user clicks a Search button that triggers a server-side Apex controller action — the button remains active while the request is processing. This can allow users to accidentally submit the same request multiple times before the first request completes, causing duplicate processing or errors.
This article explains how to disable an Apex command button when an AJAX request starts, and automatically re-enable it when the request finishes, using the apex:actionStatus component in Salesforce Visualforce pages.
The solution uses the apex:actionStatus component to monitor the lifecycle of an AJAX request. This component fires onstart and onstop JavaScript event handlers that directly control the button's enabled/disabled state via the DOM.
Key components used:
apex:actionStatus — monitors AJAX request state and triggers JavaScript eventsapex:commandButton — the button to be disabled/re-enabled; references the status component via its status attributeDisableButton() — JavaScript function that retrieves the button by its component ID and sets btn.disabled = trueEnableButton() — JavaScript function that reverses this by setting btn.disabled = falseThe Visualforce page structure includes an input text field for search, a command button that triggers the search action, and an action status component. When the AJAX request starts (user clicks Search), the DisableButton() function fires. When the request completes, EnableButton() fires and restores the button. The rerender attribute on the command button refreshes only the results section, not the entire page.
(See Visualforce code example below)
<apex:page controller="exampleCon" id="myPage">
<script type="text/javascript">
function DisableButton() {
var btn = document.getElementById("{!$Component.myPage.myForm.myPageBlock.myPageBlockSection.myPageBlockSectionItem.btnSearch}");
btn.disabled = true;
}
function EnableButton() {
var btn = document.getElementById("{!$Component.myPage.myForm.myPageBlock.myPageBlockSection.myPageBlockSectionItem.btnSearch}");
btn.disabled = false;
}
</script>
<apex:form id="myForm">
<apex:pageBlock mode="edit" id="myPageBlock">
<apex:pageBlockSection id="myPageBlockSection">
<apex:pageBlockSectionItem id="myPageBlockSectionItem">
<apex:outputLabel for="searchText">Search Text</apex:outputLabel>
<apex:panelGroup id="myPanelGroup">
<apex:inputText id="searchText" value="{!searchText}"/>
<apex:commandButton status="AjaxStatus" action="{!doSearch}" id="btnSearch" rerender="results" value="Search"/>
<apex:actionStatus id="AjaxStatus" onstart="DisableButton()" onstop="EnableButton()" ></apex:actionStatus>
</apex:panelGroup>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection title="Results" id="results" columns="1">
<apex:pageBlockTable value="{!results}" var="item" rendered="{!HasResults}">
<apex:column value="{!item.name}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
The Apex controller contains two methods:
DoSearch() — runs a SOSL query against the Contact object using the text entered by the user, returning Name, Email, and Phone fieldsgetHasResults() — returns true only when the results list is non-empty, controlling whether the results table is rendered(See Controller code example below)
public class exampleCon {
public String searchText { get; set; }
public List<Contact> results { get; set; }
public void DoSearch() {
results = (List<Contact>)[FIND :searchText RETURNING Contact(Name, Email, Phone)][0];
}
public boolean getHasResults() {
return results != null && results.size() > 0;
}
}
Component IDs used in document.getElementById() must match the exact rendered component hierarchy in your Visualforce page. If the page structure differs from the example, update the ID path accordingly.
000385780

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.