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
Tim AndrewsTim Andrews 

Need to redirect from one Visualforce page to another on record save (insert)

I have a custom object called Public_Information_Request__c that is exposed in my Community site (Napili template) via a VF page, called PIARequest. I am new to VF, so what I need help with is this: Once the user submits (saves) the Public Information Request record, instead of the record detail page becoming visible, I want to redirect the user to another VF page, called PublicInformationRequestSuccessful. This page displays the success message, as well as output fields with the Name, Requestor First Name, Requestor Last Name and Subject of Request data.

Below are my VF pages and the controller that is supposed to trigger the display of the success page:

PIARequest VF Page:
 
<apex:page standardController="Public_Information_Request__c" showheader="false" sidebar="false">
    <apex:pageMessages />
    <apex:form >
        
        <apex:pageBlock title="New Public Information Request">
            
            <apex:pageBlockSection columns="1">
    			<apex:inputField value="{!Public_Information_Request__c.Name}"/>
        		<apex:inputField value="{!Public_Information_Request__c.Date_Request_Submitted__c}"/>
        		<apex:inputField value="{!Public_Information_Request__c.Requestor_First_Name__c}"/>
        		<apex:inputField value="{!Public_Information_Request__c.Requestor_Last_Name__c}"/>
        		<apex:inputField value="{!Public_Information_Request__c.Requestor_Email__c}"/>
        		<apex:inputField value="{!Public_Information_Request__c.Requestor_Phone__c}"/>
        		<apex:inputField value="{!Public_Information_Request__c.Subject_of_Request__c}"/>      		
            </apex:pageBlockSection>
            
            <apex:pageBlockButtons >
                <apex:commandButton value="Submit Request" action="{!save}" />
            </apex:pageBlockButtons>
        
        </apex:pageBlock>
        
    </apex:form>
</apex:page>

Controller:
 
public with sharing class MyPageController {

    Public_Information_Request__c request;
        
        private ApexPages.StandardController controller;
   		 public MyPageController(ApexPages.StandardController controller) {
            this.controller = controller;
        
    }
    
    public PageReference saveAndSuccess() {
        if(controller.save() !=null) {
        PageReference successPage = Page.PublicInformationRequestSuccessful;
        successPage.setRedirect(true);
        successPage.getParameters().put('id',controller.getId());
        return successPage;
        } return null; 
	}
}

PublicInformationRequestSuccessful VF Page:
 
<apex:page standardController="Public_Information_Request__c" showheader="false" sidebar="false" extensions="PublicInformationRequest"> 
  <apex:sectionHeader title="Public Information Request Request Received" subtitle="Thank You"/>

  <apex:form >
    <apex:pageBlock title="Public Information Request Details">

      <apex:pageBlockSection columns="1">
        <apex:outputField value="{!Public_Information_Request__c.name}"/>
        <apex:outputField value="{!Public_Information_Request__c.Requestor_First_Name__c}"/>
        <apex:outputField value="{!Public_Information_Request__c.Requestor_Last_Name__c}"/>
        <apex:outputField value="{!Public_Information_Request__c.Subject_of_Request__c}"/>    
        </apex:pageBlockSection>
        
        <apex:pageBlockButtons >
        <apex:commandButton value="Save" action="{!save}" id="saveButton" />
        </apex:pageBlockButtons>

    </apex:pageBlock>
  </apex:form>
</apex:page>

I would sure appreciate any help that I can get to make this work!
Thank you!

 
SandhyaSandhya (Salesforce Developers) 
Hi Tim,


As you are using standard Save functionality in command button, this will redirect you to detail page.To redirect to another Visualforce page, you need to write custom save logic.As you are using Standard controller, make use of extensions where you can write your custom logic for save.

Below is the updated code.
These are the changes i made.
extensions="MyPageController"

<apex:commandButton value="Submit Request" action="{!saveAndSuccess}" />


public PageReference saveAndSuccess() {
insert request;

}

VFPage1
------------
<apex:page standardController="Public_Information_Request__c" showheader="false" sidebar="false" extensions="MyPageController">
    <apex:pageMessages />
    <apex:form >
        
        <apex:pageBlock title="New Public Information Request">
            
            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!Public_Information_Request__c.Name}"/>
                <apex:inputField value="{!Public_Information_Request__c.Date_Request_Submitted__c}"/>
                <apex:inputField value="{!Public_Information_Request__c.Requestor_First_Name__c}"/>
                <apex:inputField value="{!Public_Information_Request__c.Requestor_Last_Name__c}"/>
                <apex:inputField value="{!Public_Information_Request__c.Requestor_Email__c}"/>
                <apex:inputField value="{!Public_Information_Request__c.Requestor_Phone__c}"/>
                <apex:inputField value="{!Public_Information_Request__c.Subject_of_Request__c}"/>           
            </apex:pageBlockSection>
            
            <apex:pageBlockButtons >
                <apex:commandButton value="Submit Request" action="{!saveAndSuccess}" />
            </apex:pageBlockButtons>
        
        </apex:pageBlock>
        
    </apex:form>
</apex:page>


Extension
--------------
 
public with sharing class MyPageController {

    Public_Information_Request__c request;
        
        private ApexPages.StandardController controller;
         public MyPageController(ApexPages.StandardController controller) {
            this.controller = controller;
        
    }
    
    public PageReference saveAndSuccess() {
    insert request;
        if(controller.save() !=null) {
        PageReference successPage = Page.PublicInformationRequestSuccessful;
        successPage.setRedirect(true);
        successPage.getParameters().put('id',controller.getId());
        return successPage;
        } return null; 
    }
}

VFPage2
---------------
<apex:page standardController="Public_Information_Request__c" showheader="false" sidebar="false" extensions="PublicInformationRequest"> 
  <apex:sectionHeader title="Public Information Request Request Received" subtitle="Thank You"/>

  <apex:form >
    <apex:pageBlock title="Public Information Request Details">

      <apex:pageBlockSection columns="1">
        <apex:outputField value="{!Public_Information_Request__c.name}"/>
        <apex:outputField value="{!Public_Information_Request__c.Requestor_First_Name__c}"/>
        <apex:outputField value="{!Public_Information_Request__c.Requestor_Last_Name__c}"/>
        <apex:outputField value="{!Public_Information_Request__c.Subject_of_Request__c}"/>    
        </apex:pageBlockSection>
        
        <apex:pageBlockButtons >
        <apex:commandButton value="Save" action="{!save}" id="saveButton" />
        </apex:pageBlockButtons>

    </apex:pageBlock>
  </apex:form>
</apex:page>

Please let me know if you need more help!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya


 
Tim AndrewsTim Andrews
Thank you Sandhya! I am now seeing this rather nebulous error message on the controller in the force.com dev console: "duplicate value found: <unknown> duplicates value on record with id: <unknown>" ??? I've Googled this error but can't seem to track down the cause.
Tim AndrewsTim Andrews
Sandhya, I was able to resolve the error above on my own. However, I am now getting this error with the code you provided: 

System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!saveAndSuccess}' in component <apex:commandButton> in page piarequest: Class.MyPageController.saveAndSuccess: line 12, column 1
Class.MyPageController.saveAndSuccess: line 12, column 1
SandhyaSandhya (Salesforce Developers) 
Hi Tim,

Please update your Mypage Controller as below.
 
public with sharing class MyPageController {

    Public_Information_Request__c request;
        
        private ApexPages.StandardController controller;
         public MyPageController(ApexPages.StandardController controller) {
    this.controller = (Public_Information_Request__c)stdController.getRecord();
           
        
    }
    
    public PageReference saveAndSuccess() {
    insert request;
        if(controller.save() !=null) {
        PageReference successPage = Page.PublicInformationRequestSuccessful;
        successPage.setRedirect(true);
        successPage.getParameters().put('id',controller.getId());
        return successPage;
        } return null; 
    }
}

Hope this helps you!

If this solves your question please mark it as solved.

Thanks and Regards
Sandhya