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
jmickjmick 

Redirect to visual force page after new record save on site

Hi Everyone,

 

I'm new to working with Apex code and I have looked around for the answer to this but everyone seems to have a slightly different situation. I have the fields for a custom object displayed on a site that I want public users to be able to use to create new records. However, when I submit a new record it takes me to a logon page because it is attempting to take me to the new record. Instead I want it to take users to a page that says they successfully created a record. I have created a controller extension to override the save action but something is wrong with it as it does not even work on the VF page, I get the error "System.NullPointerException: Attempt to de-reference a null object" "Class.MyPageController.save: line 8, column 8 External entry point" , where line 8 is the "Insert request" line. I have been working from information on the two sites below. Any help would be greatly appreciated.

 

http://blog.jeffdouglas.com/2008/11/14/redirecting-users-to-different-visualforce-pages/

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_pages_pagereference.htm

 

Site Code

<apex:page sidebar="false" showHeader="false" standardController="Request__c" extensions="MyPageController">   
<apex:form >
    <apex:pageBlock >
    <apex:pageMessages />
    <apex:pageBlockSection >
    <apex:detail />
        <apex:inputField value="{! Request__c.First_Name__c}" />
        <apex:inputField value="{! Request__c.Last_Name__c}"/>
        <apex:inputField value="{! Request__c.Email_Address__c}"/>
        <apex:inputField value="{! Request__c.Cost_Center__c}"/>
        <apex:inputField value="{! Request__c.Department__c}"/>
        <apex:inputField value="{! Request__c.Director_s_Email__c}"/>
        <apex:inputField value="{! Request__c.Director_s_First_Name__c}"/>
        <apex:inputField value="{! Request__c.Director_s_Last_Name__c}"/>
        <apex:inputField value="{! Request__c.Mailstop__c}"/>
        <apex:inputField value="{! Request__c.Phone_Number__c}"/>

    </apex:pageBlockSection>
    </apex:PageBlock>       
        <apex:commandButton action="{! save}" value="Save"/>
       
    </apex:form>
</apex:page>

 

Controller Extension

public class MyPageController {
Request__c request;
private ApexPages.StandardController controller;
public MyPageController(ApexPages.StandardController controller) {
this.controller = controller;
}
public PageReference save() {
insert request;
PageReference requestPage = Page.Congratulations;
requestPage.setRedirect(true);
return requestPage;

}

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Two problems here:

 

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.

2) "request" is null, because you never assigned a value to it. You don't need it anyways, because you can reference it directly from the controller.

 

Here's how I would correct the two items:

 

 

public PageReference saveAndCongrat() {
  controller.save(); // This takes care of the details for you.
  PageReference congratsPage = Page.Congratulations;
  congratsPage.setRedirect(true);
  return congratsPage;
}

Finally, change the "{!save}" value of the action attribute to "{!saveAndCongrat}", remove the extra variable, and you're done.

 

 

All Answers

sfdcfoxsfdcfox

Two problems here:

 

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.

2) "request" is null, because you never assigned a value to it. You don't need it anyways, because you can reference it directly from the controller.

 

Here's how I would correct the two items:

 

 

public PageReference saveAndCongrat() {
  controller.save(); // This takes care of the details for you.
  PageReference congratsPage = Page.Congratulations;
  congratsPage.setRedirect(true);
  return congratsPage;
}

Finally, change the "{!save}" value of the action attribute to "{!saveAndCongrat}", remove the extra variable, and you're done.

 

 

This was selected as the best answer
jmickjmick

That did the trick. Thanks so much for your help. I assumed that the save method would override the standard save but I guess not and I probably never would have figured out the controller.save so thanks again.

BrettBBrettB

great solution...

How would I add the newly created on the end of the new Redirected SaveandCongarat Page?

Thanks in advance.

sfdcfoxsfdcfox

Go ahead and add the parameter for the record to the pageReference so you can query it later:

 

public PageReference saveAndCongrat() {
  if(controller.save() != null) {
    PageReference congratsPage = Page.Congratulations;
    congratsPage.setRedirect(true);
    congratsPage.getParameters().put('id',controller.getId());
    return congratsPage;
  }
return null; }

BTW, We need to make sure that the standard save method doesn't return null (there were no errors), which I forgot to include before. But once you're on the new page, you can query ApexPages.currentPage().getParameters().get('id') so you can link to the new page.

vishal chaudhary 16vishal chaudhary 16

hi it solved my problem also thanx for that,

but what would be its test class.
how can we write test class for this extension

Syed NoormohamadSyed Noormohamad
@isTest
public class RequestTest{
Request__c  req= new Request__C();
       req.Last_Name__c=
        req.Email_Address__c='';
        req.Cost_Center__c=
     req.Department__c=
        req.Director_s_Email__c=
        req.Director_s_First_Name__c=
        req.Director_s_Last_Name__c=
       req.Mailstop__c=
        req.Phone_Number__c=


}
Syed NoormohamadSyed Noormohamad
@isTest
public class RequestTest{
//create test data.
public static testMethod void testRunAs(){
Request__c  req= new Request__C();
       req.Last_Name__c='Noor';
        req.Email_Address__c='Hyderabad';
        req.Cost_Center__c='Madhapur';
        req.Department__c='IT';
        req.Director_s_Email__c='abc@forsysinc.com';
        req.Director_s_First_Name__c='test';
        req.Director_s_Last_Name__c='lastnametest';
        req.Mailstop__c='XXX';
        req.Phone_Number__c=9999;

insert req;
}
​Test.StartTest();
testRunAs();
MyPageController.save();
Test.StopTest();


}
WminWmin
Hi all, is there a way to display field validation errors and redirect only if data is accepted? 
Sudeep SinghSudeep Singh
How to develop a form using visualforce page ?
That form should be functionable. One button should be there in record page that will open that visualforce page. Once clicked that button then the latest update data in that record page of different fields must be automatically get written in that form. If checkbox is there then in form the checbox must be ticked. 

This form will be rendered as pdf format.

I need help on it.

Thanks