Apex:commandButton and onClick issue...

Friday, October 2, 2009 by Aslam - The Alexendra
Hi,
Sometimes we find issues when we use <apex:commandButton> with both onClick and Action. We try to return false / true from our javascript method and expect things to be happen properly but sometimes it not happens. See below:-

<script>
function confirm(){
if(something condition){
return true;
}
return false;
}
</script>

<apex:commandButton value="Submit" action="{!CallAction}" onClick="return confirm();"/>

The above code does not call action sometimes..(Dont know why)...so if you change it with this, it will work:-
<apex:commandButton value="Submit" action="{!CallAction}" onClick="if(!confirm()) return false;"/>

The above code will work....Please don't ask why........


Thanks
Posted in | 6 Comments »

6 comments:

Anonymous said...

This worked for me. Thanks! --RP

SpinozaQ said...

If you take a look at the generated coded you'll see why this is. SalesForce appends the AJAX call to the onclick handler. So when you return true ( or anything else ) from the onclick the AJAX call never happens. By only returning when the confirm fails the correct behavior happens.

Sangam R said...

Good one..! Very helpful. And it worked for me. thanks to all

Anonymous said...

Thanks it helped

Anonymous said...

Big thanks for this tip. Using Winter 14 and this still is a bug.

Anonymous said...

Great! it worked for me too

Post a Comment