+ Start a Discussion
Rakesh KumarRakesh Kumar 

Call multiple apex controller methods after click Javascript Function using Command Link

 

Hi All,

 

I would like to call apex controller method when we move one page to another page. I have to also check data modification on Visualforce page then display Confirm Dailog box. If User Click 'Yes' then modified data get saved in table, if user click  'Cancel'  of dailog box then it will move for next page without any data save.

 

Code Sample::

var flag='false';

   function goPage(pages){
           if(flag=='true'){
               if(confirm('Before move to the next page. Do you want to save modified Status? ')== true){
                saveMePermanent(); // Simaple Javascript Funciton
                nextAfterSave(); // This is apex controller method
            }
        }
           document.getElementById("pNumber").value = pages;
           nextBeforeSave(); // this is apex controller method
   }

 

 

Visual force Page



 <apex:actionFunction name="nextAfterSave" action="{!submitStatus}" />
 <apex:actionFunction name="nextBeforeSave" action="{!next}" />

 

<table >
        <tr>
            <td>
              <apex:panelGrid columns="50">
              <apex:repeat value="{!activePages}" var="pages">
                &nbsp;
                <apex:commandLink onclick="goPage({!pages});" >{!pages}</apex:commandLink>
                
                &nbsp;
              </apex:repeat>
              <input type="hidden" name="pageNumber" value="" id="pNumber"/>
            </apex:panelGrid>
            </td>
            
        </tr>  
    </table>

 

I dont know why its not working. I have seen some sample code also that have written as i have given in sample.

 

Is i am missing something in this code?

Please help me.

 

Thanks in Advance

 

R.K

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Bob's definitely correct here. You need to prevent bubbling up to the internal library's default postback link handler, which is a form submission that refreshes the entire page (or named elements if using reRender as well). So, changing the attribute to: "goPage({!pages}); return false;" should be sufficient. 

All Answers

bob_buzzardbob_buzzard

I think you need to add a return false; statement to the end of your goPage function.  If you don't the actionfunction will fire and then the commandlink will continue to postback the page, putting you into an interesting race condition.  By returning false, you tell the browser not to complete the default action associated with the link.

sfdcfoxsfdcfox

Bob's definitely correct here. You need to prevent bubbling up to the internal library's default postback link handler, which is a form submission that refreshes the entire page (or named elements if using reRender as well). So, changing the attribute to: "goPage({!pages}); return false;" should be sufficient. 

This was selected as the best answer
Nandu123Nandu123

Hi,

i am also having same requirement. but i am not getting the proper order of the code. So please send me the complete code. Means VF page and its controller also. still u are getting any errors, i can rectify them.