Loading

Disable or enable a command button after start and finish of Ajax request

Julkaisupäivä: Jun 22, 2026
Kuvaus

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.

Ratkaisu

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 events
  • apex:commandButton — the button to be disabled/re-enabled; references the status component via its status attribute
  • DisableButton() — JavaScript function that retrieves the button by its component ID and sets btn.disabled = true
  • EnableButton() — JavaScript function that reverses this by setting btn.disabled = false

Visualforce Page Setup

The 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>

Controller Class

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 fields
  • getHasResults() — 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;
    }
}

Important Note

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.

Knowledge-artikkelin numero

000385780

 
Ladataan
Salesforce Help | Article