[SalesForce] Command button and Action Function

I want the action function to be called when command button is clicked which calls an apex class.

The debug log is shoing everthing working fine but the page is not redirected.

<apex:form >
<apex:outputpanel>
<apex:actionFunction name="lnkNavClicked" action="{!openforms}" rerender="none" >
    <apex:param name="p1" assignTo="{!linkclicked}" value=""/>
<apex:actionFunction>
</apex:outputpanel>
<apex:outputpanel>
<table>
<tr>
<td>
<apex:commandButton value="PREVIOUS" action="{!Previous}" disabled="true"/>
<apex:commandButton value="NEXT" onclick="return lnkNavClicked('ContactDetailsPage');" />

</td>
</tr>
</table>
</apex:outputpanel>
</apex:form>

public pageReference openforms()
    {
        System.debug('## in openContactDetails');
        this.application = application;
        update (this.application);
        Id id = System.currentPageReference().getParameters().get('id');
        System.debug('## linkclicked' + linkclicked);
        PageReference pageRef = new PageReference('/' + linkclicked);
        pageRef.getParameters().put('id',id);

        System.debug('## pageRef' + pageRef);
        pageRef.setRedirect(true);
        System.debug('## pageRef.getRedirect()' + pageRef.getRedirect());

        return pageRef;

    }

I debugged this… In debug log.. Its giving all values… but not redirecting to that page … what could be the issue

following are some parts of lines from debug log..

12:55:08.691 (691845000)|CODE_UNIT_STARTED|[EXTERNAL]|01pi0000003MRR3|NewCustomerAccountController invoke(openforms)
12:55:08.691 (691932000)|SYSTEM_MODE_ENTER|false
12:55:08.691 (691968000)|SYSTEM_METHOD_ENTRY|[157]|System.debug(ANY)
12:55:08.691 (691997000)|USER_DEBUG|[157]|DEBUG|## in openContactDetails
12:55:08.692 (692155000)|DML_BEGIN|[159]|Op:Update|Type:Application__c|Rows:1
12:55:08.771 (771775000)|USER_DEBUG|[161]|DEBUG|## linkclickedOAOAContactDetailsPage
12:55:08.772 (772077000)|USER_DEBUG|[165]|DEBUG|## pageRefSystem.PageReference[/ContactDetailsPage?id=a02i0000008LN7aAAG]
12:55:08.772 (772304000)|USER_DEBUG|[167]|DEBUG|## pageRef.getRedirect()true
12:55:08.772 (772316000)|SYSTEM_METHOD_EXIT|[167]|System.debug(ANY)
12:55:08.772 (772331000)|SYSTEM_MODE_EXIT|false
12:55:08.782 (782934000)|CODE_UNIT_FINISHED|OAOANewCustomerAccountController invoke(openforms)
12:55:08.783 (783885000)|VF_APEX_CALL|j_id38|{!openforms}|PageReference:/ContactDetailsPage?id=a02i0000008LN7aAAG
12:55:08.214 (785676000)|CUMULATIVE_LIMIT_USAGE

Best Answer

It won't work like this and here is why: Your action in the actionFunction returns a pagereference. But you also specify a rerender (Which you need to pass the parameter). This rerender prevents the page from redirecting because it rerenders a non-existing part of the page.

What you can do is not use an actionfunction but change the commandButton to:

<apex:commandButton action="" value="NEXT">
    <apex:param name="p1" assignTo="{!linkclicked}" value="ContactDetailsPage" />
</apex:commandButton>

Just another thought that just popped in my head about your original code you put in your question: You might try to put somewhere in your page:

<apex:outputPanel id="hiddenPannel" style="display:none;" />

And change the rerender="none" to rerender="hiddenPannel"