[SalesForce] how to call another visualforce page from an apex command button

I have an Apex command button in my Visual Force page. Using that button I want to call another visual force page, which in turn should call action function written in the controller.

Please advise.

Best Answer

Put the below code in the controller action method that is being called on click of command button


PageReference pr = new PageReference('/apex/yourVFPageName');
pr.getParameters().put('key','value');
pr.setRedirect(true); // If you want a redirect. Do not set anything if you want a forward.
return pr;

Edit: In the constructor of the 'yourVFPageName' page controller you can call any method.

As @Vigneshwaran G has mentioned:

use action attribute in page to call that method. Otherwise if the method being called from constructor contains any DML statements it will throw error.

Related Topic