function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SteveAtGFISteveAtGFI 

Is it possible to redirect from a controller?

I have a page controlled by a controller that need to check if a case exists, and if it does, redirect. I'm calling:

 

 redirectPage = new PageReference('http://mypagehere.com');

 

from a constructor, but it does nothing unless I action it with a visualforce action. I don't really want to do this. What I want to have happen is for anyone that comes to the page and has a parameter that indicates a redirect to simply redirect. A la:

 

1. User tries to go to page with "http://myorg.com/mpage?case=1

2. Controller says "If case=1, redirect" in the constructor.

 

Is there no way to do this?

Best Answer chosen by Admin (Salesforce Developers) 
RustanRustan

You will have to do an action.

 

Try something like this

 

<apex:page controller="test" action="{!test2}">
<!-- Begin Default Content REMOVE THIS -->
<h1>Congratulations</h1>
This is your new Page: test3
<!-- End Default Content REMOVE THIS -->
</apex:page>

 

 

public with sharing class test {

public PageReference test2() {

integer x = 0;

if(x == 1)
{
PageReference pageRef = new PageReference('http://www.google.com');
return pageRef;
}
else
{
return null;
}
}

}

All Answers

RustanRustan

public class controller {

public controller() {

// case query here

// if statement asking if case was found
{
redirect here if true
}

}

}

SteveAtGFISteveAtGFI

Ya - that's what I described trying to do. It does not work. Here is the a code sample.

 

 

 public CustomerSatisfactionSurveyExtension(ApexPages.StandardController stdController) 
    {
        //I even try to do it no matter what here.
        PageReference redirectPage = new PageReference('http://mypage.com');
        redirectPage.setRedirect(true);
        
        cssController = (ApexPages.StandardController)stdController;
        caseNumber = ApexPages.currentPage().getParameters().get('ccc');
        existingCase = [SELECT Id, CaseNumber, Language__c, HasBeenSurveyed__c FROM Case WHERE CaseNumber=:caseNumber];

        if (!existingCase.isEmpty())
        {
            if (existingCase[0].HasBeenSurveyed__c == true)
            {
                redirectPage = new PageReference('http://mypage.com');
                redirectPage.setRedirect(true);
            }  
        } 
        else 
        {
            LanguageChoice = existingCase[0].Language__c;
            AssignLanguageChoice(LanguageChoice);
        }
    }

 

 

RustanRustan

You will have to do an action.

 

Try something like this

 

<apex:page controller="test" action="{!test2}">
<!-- Begin Default Content REMOVE THIS -->
<h1>Congratulations</h1>
This is your new Page: test3
<!-- End Default Content REMOVE THIS -->
</apex:page>

 

 

public with sharing class test {

public PageReference test2() {

integer x = 0;

if(x == 1)
{
PageReference pageRef = new PageReference('http://www.google.com');
return pageRef;
}
else
{
return null;
}
}

}

This was selected as the best answer