You need to sign in to do that
Don't have an account?
Tim 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:
Controller:
PublicInformationRequestSuccessful VF Page:
I would sure appreciate any help that I can get to make this work!
Thank you!
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!
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
------------
Extension
--------------
VFPage2
---------------
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
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
Please update your Mypage Controller as below.
Hope this helps you!
If this solves your question please mark it as solved.
Thanks and Regards
Sandhya