Pages

Friday, June 23, 2017

Apex commandButton and onClick issue

Apex commandButton and onClick issue


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 dont ask why........


Thanks

Available link for download