6

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?

  • 1
    I have observed this lately as well that having the rerender in the action function prevents the page reference from taking you to the new page. I do not recall this happening before. – Eric Dec 6 '14 at 17:28
  • 1
    see bobbuzzard.blogspot.com/2011/07/… for alternative way of passing params to apex methods – cropredy Dec 6 '14 at 19:04
  • The alternative way does not work for me as I still need to send the user to saved record page after save button is clicked. Also, I cannot get rid of my javascript function as it is doing some important tasks. – codeinprogress Dec 6 '14 at 19:16
2

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.

-1

Yes ,this problem still persists ,but we hav work arround by involving action support and action functions this link should help you http://sfdcmaniac.blogspot.in/2015/10/passing-parameters-to-controller-and.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.