2

I have a problem in a Visualforce page for a site page...

The process involve:

  • Html + css + jquery

  • ActionFunction => with rerender, with parameters

  • Redirect from a https tio a http

jquery actually reads the data from the document, manipulates them and calls an actionFunction passing some variables. These variables are expected to be asigned to the parameters associated to the actionFuncion. Here happens the first strange thing that we are not able to understand and manage it (looks like there is some magic)

  • if you use the rerender parameter of the action function then the <apex:param> of the actionFunction are defined. If the rerender parameter is not used, the <apex:param> are not defined (it's like they do not exist for the controller). The method of the controller called by the actionFunction is a void method.

  • when completed, the action associated to the actionFunctoin, we want to redirect the page from an https to an http page. In this case we tried to use oncomplete and use it to call an JS function. We tried different solutions

    • window.top.location
    • window.top.location.replace
    • window.top.location.href
    • or similar options offered by js and internet

the problem is that all this doesn't work.

We are using actionfunction due to css issues that we have with commandButton or commandLink.

actually for the <apex:param> we can redirect a generic element of the document, but we actually are not finding a solution to redirect a page using js oncomplete...

we are not understanding how to manage this and we also don't find a clear solution on web. Anyone can help us?

We found a WA but is something magic and is not smooth as solution, we dont like it.

Our WA is:

  • instead of <apex:param> we use the html tag <input>, this allows us to create params that you can get in the controller by calling with jquery the actionFunction giving these inputs as parameters.
  • when in the actioFunction we don't use the redirect parameter, this make the action function rerender all the page, and with the script below we are able to make the redirect:

   <apex:outputPanel rendered="{!redirectPage}">
        <script>
            window.top.location.replace('http://....');
        </script>
    </apex:outputPanel>

where redirectPage is a boolean parameter which is set in the controller...

we dont know how to understand better all the underlying the actionfunction component.

Can anyone explain us how to manage all of this in a smother way?

Note We do not have time to reengineer the code in such way to use RemoteActions.

  • If I'm understanding this correctly, it sounds like you want to 1. use an actionFunction and pass params to it that are then available in the controller. 2. Use the response from the actionFunction to redirect the browser to another URL. Is this correct? – Mark Pond Mar 2 '16 at 0:08
  • Hi @MarkPond, thanks for the corrections, and sorry for my errors :/ yes, what you say is correct. We want to understand how to do it and manage it properly. For some reasons what we done works, but we don't have a clear control on it. Thanks for your help in advance. – Klodj_Tema Mar 2 '16 at 9:54
1

When using <apex:actionFunction> with <apex:param> , the actionFunction needs to do a reRender of any dummy element on the page, otherwise it will rerender all the page probably. And not only this, but any parameter you give it when calling it with javascript (ex: doSomething(value);) , will not be given to the param and the controller will read the param value as null.

reRender is key here. I dont know why, but it probably has something to do with the way Salesforce compiles the <apex:actionFunction> element.


I will explain this with a scenario.

how it will work:

Suppose you call the actionFunction with Jquery this way:

var email = $('#txtEmail').val();
doSomething(email);

With the actionFunction being:

<apex:actionFunction id="doSomething" name="doSomething" action="{!doSmth}" reRender="paneldummy" oncomplete="redirectPage()" >
        <apex:param name="emailApex" value=""/>
</apex:actionFunction>

And paneldummy being just:

<apex:outputPanel rendered="false" id="paneldummy"></apex:outputPanel>

And suppose we have a redirect function like this in the page:

function redirectPage(){
    window.top.location.replace('http://salesforce.stackexchange.com');
};

This is the scenario that will work. In this case you could call the doSmth method in the controller like this:

public void doSmth(){
   String email = ApexPages.currentPage().getParameters().get('emailApex');
   // do something else ...
}

and after it is finished the actionFunction will call the redirectPage function in js, because there is no conflicts in rerenderings.

When it will not work

If you do not reRender the dummy element or any element the actionFunction will:

1) rerender all the page

2) 'emailApex' will not exist as a parameter for the controller, INSTEAD 'txtEmail' wich was passed to it only as a value via jquery, WILL exist as a parameter, which is very WIERD, and the controller will get:

String email = ApexPages.currentPage().getParameters().get('txtEmail');

as a valid parameter, which it normally shouldn't.

3) The script in your outputpanel, will work because it will be a rendered element after all the page is rerendered, because of a logic that makes rendered="true" for the script's panel (in your case the boolean {!redirectPage} from the controller).

4) The redirectPage() function in js, will not be called because of a conflict in rerendering i think. Meaning that the actionFunction rerenders all the page before it can call the redirectPage() function.


If you do the rerender of the dummy element however, and/or the outputPanel where the script is, the script will not work, so you would need to do both the reRender to some element and the onComplete call to a js redirect function in order for it to work.

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.