[SalesForce] ActionFunction rerender messing with Pagereference

I have a javascript method on my vf page which is called by a save button. This method performs some calculations, collects some data, and sends them to the save method in my controller as parameters using actionFunction.

 <script>
     function validate() {
        var somevar1 = //some calculation
        var somevar2 = //some calculation

        save(somevar1, somevar2);
     }
</script>

<apex:actionFunction name="save" action="{!saveRecord}">
     <apex:param name="somevar1" value=""/>
     <apex:param name="somevar2" value=""/>
</apex:actionFunction>

In my controller

public Pagereference saveRecord() {
     Custom__Obj__c obj = new Custom__Obj__c();
     obj.some__field1__c = Apexpages.currentPage().getParameters().get('somevar1');
     obj.some__field2__c = Apexpages.currentPage().getParameters().get('somevar2');
     insert obj;

    Pagereference p = new Pagereference('/' + obj.Id);
    p.setRedirect(true);
    return p;
}

Now here's my problem.

If – I do not put any rerender=" " in actionFunction then when I save the record I get redirected to the saved record page, BUT those two params somevar1 and somevar2 come in as null. So I end up saving the record but with null values for those two fields.

If – I do put rerender=" " in my actionFunction then when I save the record I DO NOT get redirected to the save record page, it still keeps me in the vf page, BUT those two params somevar 1 and somevar2 come in with their values.

Is this a salesforce bug or am I doing something wrong? Is there a way out of this?

Best Answer

So I have to do a workaround for this. I am still keeping my rerender since it returns the params I need, but I am now redirecting the page using javascript.

 <apex:actionFunction name="save" action="{!saveRecord}" rerender="" oncomplete="redirect('{obj.Id}');">
    <apex:param name="somevar1" value=""/>
    <apex:param name="somevar2" value=""/>
 </apex:actionFunction>

In my script

 var redirect = function(objId) {
          window.href.location = '/' + objId;
 }

And it then redirects me to the saved object page.

Related Topic