Search code examples
salesforce

PageReference.setRedirect() is not working


Hi i am trying to redirecting visualforce page to google.com page here is my VF code

<apex:page controller="google_redir">
  <!-- Begin Default Content REMOVE THIS -->
  <h1>Congratulations</h1>
  This is your new Page
  <!-- End Default Content REMOVE THIS -->
  <apex:form >
  <apex:commandButton action="{! hello1}"  value="Save New Account Value"/>
  </apex:form>
</apex:page>

and here is my apex controller code

public class google_redir{


public PageReference hello1(){
PageReference reference=new PageReference('http://www.google.com');
reference.setRedirect(true);

return reference;
}
}

page not redirected to google.com it shows a blank page. please help me in correcting the error where i am wrong why it is not redirecting to google.com page.


Solution

  • The exact code you posted is working for me. I was redirected to http://www.google.com after clicking the Save New Account Value button. I modified your code slightly after testing, though, for readability:

    Page

    <apex:page controller="google_redir">
        <apex:form >
            <apex:commandButton action="{!hello1}"  value="Save New Account Value"/>
        </apex:form>
    </apex:page>
    

    Controller

    public class google_redir {
        public PageReference hello1() {
            PageReference reference=new PageReference('http://www.google.com');
            reference.setRedirect(true);
            return reference;
        }
    }
    

    Could it be an issue with your browser or your Internet connection? Try redirecting to a different URL like http://www.salesforce.com or using a different Internet browser, just to see if that works.