I'd think this would work just fine, but it seems to prevent the action function from firing regardless of the confirm response

<apex:commandButton value="Remove All" action="{!removeAll}" rerender="form"
     status="LoadingStatus" onclick="return confirm('Are you sure?');"/>
  • onclick="return confirm('Are you sure?');" works fine in Firefox and Chrome right now. If user says OK (Yes) an action is executed. Maybe Salesforce fixed this issue. – Andrii Muzychuk Nov 17 '16 at 9:07

I know this has been answered but I thought I would toss my hat into the ring. Taking a look at the code generated by the commandButton tag, the onclick code goes before salesforce's own code to call the controller method. What this means is

onclick="return confirm('Are you sure?');"

is always returning before calling the code to the controller method. What I have done is to wrap the return in an if statement and call it only if the condition is correct. The code below will call return if the confirmation is false.

onclick="if(!confirm('Are you sure?')){return false};"
| improve this answer | |
  • 1
    So the important thing is that if you want the salesforce action method to get called make sure any onclick javascript does NOT return, right? – Ralph Callaway May 4 '13 at 18:50
  • 1
    @Ralph exactly! The action method gets translated into a log bit of JS code and tack onto the end of the onClick. – Bob Roberts May 6 '13 at 14:56
  • 1
    very interesting... and counter-intuitive. Adding this to the list of salesforce gotcha's – NSjonas Jul 28 '14 at 23:47
  • Doesn't work for me unless you actually return false... – NSjonas Jul 16 '15 at 22:39
  • PLEASE NOTE:--- WRT the onclick JS --- void method: Action will execute --- return null: Action will execute --- return true: Action will execute --- return false: Action will NOT execute --- onclick="if(!confirm('Are you sure?')){return false};" --- is over engineered. --- return confirm('Are you sure?'); --- is more correct. – NZ Dev Mar 19 '18 at 2:09

In my case, it was returning to controller method for both options( true or false) so then i changed it to the code below and it started to work.

onclick="if(!confirm('Are you sure?')){return false;}"
| improve this answer | |

Was able to get this to work with an action function, but hopefully someone in the community might have cleaner way to handle this.

<apex:actionFunction action="{!removeAll}" name="removeAll" rerender="form" status="LoadingStatus"/>
function confirmRemoveAll() {
    if(confirm('Are you sure?')) {
    return false;
<apex:commandButton value="Remove All" onclick="return confirmRemoveAll();"/>
| improve this answer | |
  • As it stands there is no UI feedback to the user with this code. How do you receive the server response? It would be nice if AJAX was not necessary. – Marc Feb 22 '13 at 9:50
  • @Marc if you're looking to see a fully working page I'd check out some of the resources at developer.force.com. This isn't intended to be a full page, just the how to get a confirmation dialogue on a button – Ralph Callaway Feb 22 '13 at 16:08

I'd go with the way you've handled it - you cant have a javascript prompt as well as an action method invocation in the same button. You'd have to proxy it via an Action Function or alteratively use JS-Remoting.

| improve this answer | |
  • Deleted the comments here since things were getting out of hand. – metadaddy Oct 8 '12 at 21:22
  • Is this a known issue that you can't have an action method and an onclick event? Easy to work around, but would love if this was clear – Ralph Callaway Oct 9 '12 at 21:51

I've done this with jQuery...not 100% sure why it doesn't work for you, but maybe there's something going on with the VF framework attaching its own events to the onclick handler of the button (conjecture by me). If you use jQuery you can attach your own event pretty easily and I know it works.

Anyway, back to the solution...it could be useful to have the script in some common JS file that you include in many pages where you want to hesitate on taking some generic cancel or save. Then you just give your commandbutton the proper styleClass and get the behavior immediately. For example's sake I just have it all in one page.

Put jQuery in your static resource, for example, in a zip called res in a subfolder called js and be sure to include it prior to using it.

Zip file with the following structure: /res/js/jquery-1.8.1.min.js

Referenced from VF as follows:

<apex:includeScript value="{!URLFOR($Resource.res, 'js/jquery-1.8.1.min.js')}"/>

Example code snippet from VF

<script type="text/javascript">

    jQuery(document).ready(function() {

        jQuery('.saveAction').click(function(e) {
            if ( confirm('Are you sure you want to save? Click OK to continue saving.') ) {
                return true;

<!-- somewhere on your page is your button -->
<apex:commandButton action="{!save}" value="Save" styleClass="saveAction"/>
| improve this answer | |
  • 2
    great answer, and thanks for the tip. one thing I've started doing is just loading jquery from google directly and to make things simple i put it in a component, see gist.github.com/4048689 if you're curious – Ralph Callaway Nov 9 '12 at 22:19
  • @Ralph - That's a good idea. One thing I will point out is that I have had issues with attaching the event to elements inside of an apex:pageBlockTable. Even calling the built-in return confirmDelete() directly in an onclick attribute has not worked inside of apex:pageBlockTable for me sometimes. – Peter Knolle Nov 9 '12 at 22:53
  • agreed, page block tables are full of surprises, good to know about that one – Ralph Callaway Nov 10 '12 at 0:02

I think your pretty much on the right track. The easiest way to get things into the controller is to post-back the form.

This also allows you to do field-level validation and mark-up on the VF form elements without manually adding "Errors" to input boxes.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.