2

I am calling referencepage method named Done inside another method that is being called on an command button called Save however the pagereference is not doing anything and it just stays on the same page

void save(){
    done();

    System.debug('I am after Done');

}

public PageReference done()
{
    // send the user to the detail page for the sobject
    System.debug('I am calling Done Method');
 PageReference redirectPage = new PageReference('/' + record.Id);
 redirectPage.setRedirect(true);
  return redirectPage;
} 
4

You need to return that PageReference. You can't do anything after the return.

public PageReference save()
{
    // do stuff
    return done();
}

I would also change the way you build the redirect reference to be more programmatic.

public PageReference done()
{
    return new ApexPages.StandardController(record).view();
}

Or a typical implementation of a Controller Extension might look like:

public with sharing class MyExtension
{
    final PageReference recordView;
    public MyExtension(ApexPages.StandardController controller)
    {
        this.recordView = controller.view();
        // do other stuff
    }
}
| improve this answer | |
  • Thank you so much for the respond, its working now. So you cant have a regular method call a PageReference Method? .if the record ID is from a pdf file how do you force the page reference to preview the pdf fiel PageReference redirectPage = new PageReference('/' + record.Id); – Metawaa Dec 23 '15 at 20:01
  • If you want the save method to redirect, it has to return a PageReference. Simple as that. – Adrian Larson Dec 23 '15 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.