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

possible to call pagereference from apex?

On a button click I want to call a method,perform some logic, and then call a pagereference (used elsewhere on the page as well) that then generates a redirect. Is it possible to call a PageReference from apex? If so what is the syntax to do so?




is the return type of the method a PageReference?


You can do the following:


PageReference newpage = new PageRefernce(YOUR URL);
newpage.setRedirect(true);  // optional if you want to lose view_state
return newpage;


Ankit AroraAnkit Arora

If you want to call a method from controller and after performing some logic redirect to some URL then here is the code :




<apex:page controller="MyController">
            <apex:commandButton value="Hit Me" action="{!MyMethod}"/>


Class :


public class MyController {

    public PageReference MyMethod()
        System.debug('Test Debug..... Do some logic here') ;
        PageReference pageRef = new PageReference('') ;
        return pageRef;




Ankit Arora

Blog | Facebook | Blog Page


I guess I had a similar problem and Ankit method would solve your problem, however If you want that pagereference you would have to use <apex:commandlink> tag with the attribute target ="_blank". Rest All remain same.