How to Disable CommandButton After First Click in VF?

Question
When developing a Visualforce page, you may encounter a scenario where users click a commandButton
multiple times before the page completes its redirect or processing action. This can cause multiple submissions, leading to potential data issues or performance problems. How can you ensure that the button gets disabled after the first click, preventing double submissions while also allowing a seamless redirect or processing flow?
Answer
Solution 1: Using JavaScript to Disable the Button
One of the most effective ways to disable the button after the first click is by leveraging JavaScript. This approach ensures that users cannot click the button again until the process has completed, without interfering with the redirect behavior.
CRS Info Solutions provides hands-on Salesforce training in Gurgaon, emphasizing real-world scenarios. Sign up for a free demo and begin your Salesforce journey today..!!
Step-by-Step Solution:
1. Add JavaScript to Disable the Button
You can use a JavaScript function that disables all buttons with a specific CSS class. The button is disabled when clicked, and the form is posted without a double submission.
2. Use apex:actionFunction
to Post the Form
In your Visualforce page, you will use the apex:actionFunction
tag to invoke the server-side method and handle the form submission via AJAX. The oncomplete
event will re-enable the buttons once the form has been submitted successfully.
Example:
<apex:form>
<!-- ActionFunction to post the form and handle button disabling -->
<apex:actionFunction name="doSomeWorkActionFunction"
action="{!yourControllerMethod}"
oncomplete="buttonsEnabled(true);"
rerender="rerenderElement"></apex:actionFunction>
<!-- CommandLink with onClick handler to disable the button -->
<apex:commandLink action="{!yourControllerMethod}"
value="Save"
id="theCommandLink"
onclick="return doSomeWork();" />
</apex:form>
In this setup:
1. The JavaScript function doSomeWork()
posts the form by calling doSomeWorkActionFunction()
, disables the buttons, and returns false
to prevent multiple submissions.
2. The oncomplete
event of the apex:actionFunction
re-enables the buttons after the action is successfully processed.
Explanation:
This solution prevents the form from being submitted multiple times while maintaining the redirect behavior of the controller. The button is disabled immediately after the first click, and users cannot click it again until the process completes.
Solution 2: Using apex:actionStatus
(With Limitations)
Another option is to use the apex:actionStatus
tag, which allows you to display a status message and disable the button during the action. However, this approach may not be ideal if you need to redirect to another page, as it triggers a full postback.
Example Implementation:
<apex:actionStatus id="saveStatus">
<!-- Stop status - Button is disabled during processing -->
<apex:facet name="stop">
<apex:commandButton value="Save"
action="{!save}"
status="saveStatus"
rerender="saveParentBlock" />
</apex:facet>
<!-- Start status - Disabled button with a 'Saving...' text -->
<apex:facet name="start">
<apex:commandButton value="Saving..."
disabled="true"
status="saveStatus"/>
</apex:facet>
</apex:actionStatus>
How This Works:
The apex:actionStatus
manages the state of the button. The button displays the “Saving…” text and is disabled when the form is processing.
However, it does not trigger a redirect like the JavaScript method does, as the rerender
causes a full page refresh.
Limitation:
This method involves a full postback, which prevents the PageReference
redirect and the seamless user experience you may want.
Conclusion
For most use cases, using JavaScript to disable the button after the first click is the recommended solution, as it allows for smoother handling of form submissions and redirects. The use of apex:actionStatus
can be a valid alternative, but it introduces limitations with page redirects due to full postbacks. By implementing the JavaScript-based solution, you can easily prevent multiple form submissions and improve the overall user experience.
At CRS Info Solutions, I offer an in-depth Salesforce Training in Gurgaon, designed for beginners like you. I’ll guide you step-by-step through the learning process with a focus on hands-on experience and real-world applications. You’ll gain conceptual clarity, practical skills, and expert-led insights that will empower you to confidently pursue Salesforce certification and career advancement.
With CRS Info Solutions, you’ll explore diverse Salesforce modules, including Admin, Developer, Integration, and Lightning Web Components (LWC), taught by experienced instructors. The curriculum combines theory and hands-on practice, ensuring you’re well-prepared for industry demands. Take advantage of our career-driven approach and join us for a free demo to start your journey toward success today!