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
Imran Rahman 3Imran Rahman 3 

Redirecting 'new record' VF page after 'save' to custom page with ID parameter

I have a visualforce page with a standard controller which creates a new record on click of my commandButton. This function works using just standard controllers at the moment.

What I would like to do if insertion is successful, to redirect the page to my own custom detail page (mysite.force.com/detailPage?id=) instead of the salesforce's default one. I need to also pass the ID parameter of the newly created record so I can pull up the infomation on the page.
Best Answer chosen by Imran Rahman 3
Abhishek_PareekAbhishek_Pareek
This can be done by creating your own save method in controller. The code snippet will be something like this:
public class yourSampleClass{

          ApexPages.StandardController stdController;
          public yourSampleClass(ApexPages.StandardController con){
              stdController = con;
          }
 
          public PageReference save() {
              stdController.save();
             PageReference pg = Page.detailPage;
             pg.getParameters().put('Id',stdController.Id);
            return pg;
         }


}

Hope this helps.

All Answers

Abhishek_PareekAbhishek_Pareek
This can be done by creating your own save method in controller. The code snippet will be something like this:
public class yourSampleClass{

          ApexPages.StandardController stdController;
          public yourSampleClass(ApexPages.StandardController con){
              stdController = con;
          }
 
          public PageReference save() {
              stdController.save();
             PageReference pg = Page.detailPage;
             pg.getParameters().put('Id',stdController.Id);
            return pg;
         }


}

Hope this helps.
This was selected as the best answer
Imran Rahman 3Imran Rahman 3
@Abhishek_Pareek

I have used the following code:
public class force_NewOrderLogic {

          ApexPages.StandardController stdController;
          public force_NewOrderLogic(ApexPages.StandardController con){
              stdController = con;
          }
 
          public PageReference save() {
              stdController.save();
             PageReference pg = Page.force_DetailPage;
             pg.getParameters().put('Id',stdController.Id);
            return pg;
         }

}


But I get the following error:
Expression of type ApexPages.StandardController has no member named Id at line 11 column 42
Abhishek_PareekAbhishek_Pareek
Try using stdController.getId() instead of stdController.Id
Imran Rahman 3Imran Rahman 3
@Abhishek_Pareek
Okay that allowed me to save the class, however another error has occured. When pressing the same command, the page is redirect to a page which states the following:
Error: Error occurred while loading a Visualforce page.
Imran Rahman 3Imran Rahman 3
I should add that the DetailPage should have the correct permissions, as it can be accessed directly by manually typing the url with a valid ID.
Abhishek_PareekAbhishek_Pareek
Modify this statement with your page name.i.e, Replace "force_DetailPage" with "yourpagename":
PageReference pg = Page.force_DetailPage;

Imran Rahman 3Imran Rahman 3
@Abhishek_Pareek
The correct page name has been used but I am still receiving the error.