+ Start a Discussion
Rajesh ShahRajesh Shah 

onclick and oncomplete when used together do not work for commandLink

Hi, 

I have a commandLink on click of which I want to ask the user for confirmation first i.e. whether he is sure to take the action. Once the user says Yes and the action is completed, I want a alert saying that the action was completed successfully. In order to achieve this, I used the onclick and oncomplete attributes of CommandLink. My issue is that if I use both the attributes, my commandLink action never gets fired. If I use only one of the them, everything works fine except I have only one of the popups. 


Is there an issue when both the onclick and oncomplete attribute are used together? Following is a sample code:

 

<apex:commandLink value="Test" action="{!myAction}" onclick="return window.confirm('Are you sure?');" oncomplete="alert('Event Completed');" />

 

 

bob_buzzardbob_buzzard

According to the docs, oncomplete will fire when an Ajax request is completed.  Given that you aren't rerendering anything, I suspect your request will be considered to be a simple postback resulting in a page response from the server.

 

Try adding a rerender attribute and see if that helps.

 

 

Rajesh ShahRajesh Shah

Added the rerender attribute but it didn't helped. 

bob_buzzardbob_buzzard

Hmm.  Are you rerendering the section where your commandlink is located?  

 

When I've needed to do this kind of thing I've tended to use an actionstatus with the onstart/onstop event handlers. 

jwhartfieldjwhartfield

Try using this instead -  so you don't return anything if it's ok to continue.  I think otherwise it short-circuits the built in SFDC javascript.

onclick="if(!window.confirm('Are you sure?')) return false;
sunny solanki 6sunny solanki 6
<apex:commandLink onclick="window.confirm('Are you sure?');"  oncomplete="alert('Event Completed');"  > will work fine like charm.  Just remove return attribute and your code work fine.