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
philanthropyphilanthropy 

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?

 

Thanks!

sfdcbynitesfdcbynite

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 :

 

VFP :

 

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

 

Class :

 

public class MyController {

    public PageReference MyMethod()
    {
        System.debug('Test Debug..... Do some logic here') ;
        PageReference pageRef = new PageReference('http://www.google.com') ;
        return pageRef;
    }

}

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Sam27Sam27

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.

 

thanks