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
DanteCDanteC 

Redirect to a another Salesforce page from the Constructor

How to redirect to a another Salesforce page from the Constructor?  Is there a sample code somewhere?   Somehow,  the setRedirect(true)  does not work from the Constructor of the class.
Best Answer chosen by DanteC
CJWilderCJWilder
Hi DanteC,

I'm not sure if you can do that from a constructor since they do not have an explicit return type.

You can put action attribute on your apex:page to call a methond in your class that redirects.

VF Page:
<apex:page controller="testpage" action="{!dotestpage}" >
  
  This is page just redirects to another page.
  
</apex:page>
Apex Class
public class testpage{

    public PageReference dotestpage(){
    
    PageReference pageRef = new PageReference('/apex/testtags');
    pageRef.setRedirect(true);
    return pageRef;
    
    }

}




All Answers

CJWilderCJWilder
Hi DanteC,

I'm not sure if you can do that from a constructor since they do not have an explicit return type.

You can put action attribute on your apex:page to call a methond in your class that redirects.

VF Page:
<apex:page controller="testpage" action="{!dotestpage}" >
  
  This is page just redirects to another page.
  
</apex:page>
Apex Class
public class testpage{

    public PageReference dotestpage(){
    
    PageReference pageRef = new PageReference('/apex/testtags');
    pageRef.setRedirect(true);
    return pageRef;
    
    }

}




This was selected as the best answer
Ravikant kediaRavikant kedia
<apex:page > have a action attribute this action exicute after constructor so you can redirect to another page using this  action.
DanteCDanteC
Thanks a million ++ CJWilder and Ravikant !!