+ Start a Discussion
sfdcfoxsfdcfox 

Confirmation dialog cancels action; used to not do this.

<apex:commandButton onclick="return confirm('Are you sure?')" action="{!myaction}" value="Do This!" />

 

Previously, it would ask if you wanted to confirm your action, and then do it. As of a day or two ago, using this exact method causes it to not perform the action. Removing the onclick causes it to work correctly again. Anyone else having this problem?
Best Answer chosen by Admin (Salesforce Developers) 
AmphroAmphro

I remember I had this problem a while back. I just assumed it didn't work the way you did it. Since it worked for you before, maybe this error is caused by some other code? Did you add anything a day or two ago. I would be curious to find out why that doesn't work.

 

My work around was to rephrase the statement. Try this.

 

<apex:commandButton onclick="if(!confirm('Are you sure?')) return false;" action="{!myaction}" value="Do This!" />

Let me know if that made a difference. If it does, again, I am curious why one works and the other doesn't.

All Answers

AmphroAmphro

I remember I had this problem a while back. I just assumed it didn't work the way you did it. Since it worked for you before, maybe this error is caused by some other code? Did you add anything a day or two ago. I would be curious to find out why that doesn't work.

 

My work around was to rephrase the statement. Try this.

 

<apex:commandButton onclick="if(!confirm('Are you sure?')) return false;" action="{!myaction}" value="Do This!" />

Let me know if that made a difference. If it does, again, I am curious why one works and the other doesn't.

This was selected as the best answer
sfdcfoxsfdcfox
That fixed it. So, "return confirm(xxx)" fails, but "if(!confirm(xxx) return false" works like a charm. I don't know why, but I'll try and research it. I'm okay with doing it that way, because it's easy enough. I suspect that the onclick attribute checks for a return value, and if nothing returns, then it executes. "false" is something (it's an object, albeit an object that represents nothing), but it's not "null", which is literally nothing. Just a theory, but it sounds decent enough.
k2sfdevk2sfdev

hi sfdcfox,

 

Could you try 

 

 

function confirmation(){ return confirm('Are you sure?'); }

 

 

 

onclick="return confirmation();"