+ Start a Discussion
Sylvain@RibbonFishSylvain@RibbonFish 

Custom Controller and Custom Button (Redirection)

Hi everyone,

I would like to redirect my user to a custom page with a custom controller. In this case I instantiate the custom controller to set up some parameters and would like to redirect to the controller page with the data set up.

Here the code:

VisualForce:

<apex:page standardController="Product2" extensions="Ext" action="{!returnPage}"/>

 

Apex:

public class Ext {

private final sObject mysObject;

// The extension constructor initializes the private member
// variable mysObject by using the getRecord method from the standard
// controller.
public Ext(ApexPages.StandardController stdController) {
this.mysObject = (sObject)stdController.getRecord();
}

public PageReference returnPage()
{
MycustomController cust = new MycustomController();
cust.productList.add((Product2)mysObject); //productList is a property of the controller public

return .....; //What do I should put in the return to access with the instantiated controller to the corresponding page
}
}

 

Thanks

 

Best Answer chosen by Admin (Salesforce Developers) 
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

You need to add the following code in the funciton returnPage() to redirect to a next page.

 

public PageReference returnPage()
{
MycustomController cust = new MycustomController();
cust.productList.add((Product2)mysObject); //productList is a property of the controller public
//These two lines you need to add to redirect to another VF page. E.g., SecondVF is the next VF page
PageReference nextpage = new PageReference ('/apex/SecondVF');     
return nextpage;
}
}

 

 

//If you are using any parameters to be passed to that page then follow the below code

PageReference pr = new PageReference ('/apex/SecondVF');
         pr = Page.SecondVF; // use the name of the second VF page
         pr.setRedirect(true);
         pr.getParameters().put('Param name',string.valueof(param1));
return pr;

 

Hope this will help you...!

Don't forget to give Kudos and mark this answer as a Solution if this works out.

 

All Answers

Dhaval PanchalDhaval Panchal

See below example

 

Public PageReference redirectPage(){
    PageReference P = new PageReference('/apex/myPage?id=xxxxxxx');
    Return P;
}

 

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

You need to add the following code in the funciton returnPage() to redirect to a next page.

 

public PageReference returnPage()
{
MycustomController cust = new MycustomController();
cust.productList.add((Product2)mysObject); //productList is a property of the controller public
//These two lines you need to add to redirect to another VF page. E.g., SecondVF is the next VF page
PageReference nextpage = new PageReference ('/apex/SecondVF');     
return nextpage;
}
}

 

 

//If you are using any parameters to be passed to that page then follow the below code

PageReference pr = new PageReference ('/apex/SecondVF');
         pr = Page.SecondVF; // use the name of the second VF page
         pr.setRedirect(true);
         pr.getParameters().put('Param name',string.valueof(param1));
return pr;

 

Hope this will help you...!

Don't forget to give Kudos and mark this answer as a Solution if this works out.

 

This was selected as the best answer