Using PageReference to redirect after new record insert

  • Continuing from my previous post. I want to redirect the user to a custom vf detail page when they create a new Service Order from my custom page order page (force_NewOrder).

    I have created a extension which should save and redirect the user but I am given a generic error:

    Error: Error occurred while loading a Visualforce page.

    VF (force_NewOrder)

    <apex:page showHeader="false" title="New Order" standardController="Service_Order__c" extensions="force_NewOrderLogic">
      <apex:composition template="{!$Site.Template}">
        <apex:define name="body">  
        <h2 style="margin-top:0">New Order</h2>
        <div class="row">
              <apex:form styleClass="form" >
              <apex:pageMessages id="styledError"/>
              <apex:messages id="unstyledError"/>
              <div class="row">
                <div class="col-md-6 col-xs-12">
                  <div class="row">
                    <div class="form-group col-xs-12">
                        <label for="project-name">Project Name</label>
                        <apex:inputField value="{!Service_Order__c.Name}" required="true" styleClass="form-control" />
              <div class="row">
                <div class="col-md-6 col-xs-12 text-right">
                  <a href="/force_Orders" class="btn btn-default">Cancel</a>
                  <apex:commandButton value="Create Order" action="{!save}" styleClass="btn btn-success" rerender="error,styledError,unstyledError"/> 


    public class force_NewOrderLogic {
        public force_NewOrderLogic(ApexPages.StandardController controller) {
        Service_Order__c order;
        public PageReference save() {
            insert order;
            PageReference orderPage = new PageReference('/force_OrderDetail?id=' +;
            return orderPage;

    Shouldn't your pagereference be ('/apex/force_OrderDetail?id=' + ?

    For websites, using /apex/ isnt required

    ah apologies =]

    Are you wanting your custom page to override the view for this object detail pages? If so, then there would be no need to parameterize the pageReference, just include the Id. /MYID

    @greenstork I would like the save function on my visual force page to redirect to a custom page, this is all on my force site. I would like the normal detail page to stay intact within SF

    Are you getting the error when loading this page or after clicking on the "Save" button, and if after is the URL at the top the URL you have in your "Save" method?

    @JennyB Yes the error is displayed after pressing 'Save', the URL of the page contains the error is actually the new record page and not the page that it should have redirected to.

  • Jenny B

    Jenny B Correct answer

    7 years ago

    I found an answer here that might work for you as well, where the solution would be to change your "Save" method to the following:

    public PageReference saveAndRedirect() {
      if( != null) {
        PageReference redirectPage = Page.force_OrderDetail;
        return redirectPage;
      } return null;

    Then change your action on the VF page to "saveAndRedirect".


    1) Your "save" function is shadowed by the standard controller's "save" function, so your code won't work. You have to rename the function, and call that action instead.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM