I have this following apex:

public class SampleController {

    public SampleController(){

        //I want to call the the samplepage() here.


    public PageReference samplepage(){

        PageReference pagr= new PageReference('/apex/sample?id='+sampleID);          
        return pagr;



How to call the page reference from constructor?.. Or any idea that I can direct call a vf page from the contsructor?..

1 Answer 1


You can't return a PageReference from a constructor, but you can use an action method when the page loads:

<apex:page controller="SampleController" action="{!samplepage}" ...

This will have the effect of redirecting on page load.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .