1
<apex:commandButton value="Enviar"  action="{!doSubmit}" onclick="redirect();"/>

javascript :

<script>
function abc()
{
window.location = 'www.google.com';
}
</script>

dosubmit is a method in controller which is doing case creation stuff. THe page is not getting redirected. please provide a solution

| improve this question | | | | |
  • abc() and redirect() are the same functions ? – SF_user Sep 8 '15 at 7:25
3

The apex:commandButton supports various events upon which it can call JavaScript (defined by the the on... attributes) and oncomplete is appropriate here:

<apex:page>
    <apex:form>
        <apex:commandButton value="Enviar" action="{!doSubmit}" oncomplete="abc();"/>
    </apex:form>
<script>
function abc() {
    window.location = 'https://www.google.com';
}
</script>
</apex:page>

Note that in theory your controller method could just return a new PageReference('https://www.google.com') but I can't get that to work and others have had problems with it too.

| improve this answer | | | | |
  • 1
    It would redirect in any case, either the operation done correctly or not. oncomplete will do its work. – Ashwani Sep 8 '15 at 8:30
  • @Ashwani Good point. – Keith C Sep 8 '15 at 8:37
2

You could have your dosubmit method return a PageReference instead of using javascript:

public PageReference dosubmit(){

    // create case, etc.

    PageReference pr = new PageReference('www.google.com');
    pr.setRedirect(true);
    return pr;
}

This worked for me in a Developer Org in Firefox, Chrome, IE:

<apex:page Controller="tpc" >
    <apex:form>
        <apex:commandButton action="{!dosubmit}" value="Do Submit" />
    </apex:form>
</apex:page>

controller:

public class tpc{

    public PageReference dosubmit(){

        // create case, etc.

        system.debug('test');

        PageReference pr = new PageReference('http://www.baidu.com'); // google is blocked here
        pr.setRedirect(true);
        return pr;
    }

}
| improve this answer | | | | |
  • Have you tested that - doesn't work for me in a dev org (and doesn't work for others too). – Keith C Sep 8 '15 at 7:59
  • @KeithC I read through the link, and it looks like people have just been having an issue redirecting to google. I never noticed the problem since no one here uses google and it is blocked anyways. It looks like in general this method of redirection should be fine though. – martin Sep 8 '15 at 8:52
  • I tried a couple of URLs and neither worked - unclear to me what works and what doesn't and why. – Keith C Sep 8 '15 at 9:17
1

I had to set immediate="true". Otherwise the page just seemed to do a postback.

<apex:commandButton immediate="true" value="Enviar"  action="{!doSubmit}" />

Update

After more investigation it seems like this is only needed if there is an error during the postback. In my case, I have a <apex:hiddenInput /> where the binded value was a "DateTime" field. Apparently, something about this caused my postback to fail. After removing the Element my redirect works without immediate=true.
A easy way to tell if you post back is failing is to put a System.Debug('Postback success') at the top of your action method. If you don't see this in the debug log, then you have an issue somewhere.

| improve this answer | | | | |
0

set redirect parameters in the url by using :

 &retURL=%2f<the id of the case where you want it to redirect>
| improve this answer | | | | |

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.