+ Start a Discussion
Stephanie StoddardStephanie Stoddard 

How to redirect to VF page from VF form standard controller?

I have a VF page that creates a contact, but I want the form to redirect to a Thank You page rather than try to open the newly created contact in SF.  Here is my extension coding:

public class ContactSaveExtension {

    ApexPages.StandardController controller;

    public ContactSaveExtension(ApexPages.StandardController controller) {
        this.controller = controller;
    }

    public PageReference saveAndRedirect() {
        controller.save();
        PageReference redirect = new PageReference('ApexPage.Thank_You');
        redirect.setRedirect(true);
        return redirect;
    }

}

What am I missing?

Thank you!
Best Answer chosen by Stephanie Stoddard
v varaprasadv varaprasad
Hi Stephanie

Please use below page reference class.To return another vf page syntax is ('/apex/pagename').
PageReference redirect = new PageReference('/apex/Thank_You');

Thank
Varaprasad

All Answers

v varaprasadv varaprasad
Hi Stephanie

Please use below page reference class.To return another vf page syntax is ('/apex/pagename').
PageReference redirect = new PageReference('/apex/Thank_You');

Thank
Varaprasad
This was selected as the best answer
Mukesh_SfdcDevMukesh_SfdcDev
Hi Stephanie,

In your code, you have written the wrong statement for redirect the page.
your statement is:
PageReference redirect = new PageReference('ApexPage.Thank_You');

please try this one
PageReference redirect = new PageReference('/ApexPage/Thank_You');


Thanks
Mukesh Kumar
mukesh guptamukesh gupta
Hi Stephanie Stoddard ,

I have test this code my side, Please use this 
<apex:page standardController="Contact" extensions="ContactSaveExtension">

   <apex:form >
    <apex:pageBlock >
    <apex:pageBlockSection >
     <apex:inputField value="{!Contact.FirstName}"/>
     <apex:inputField value="{!Contact.LastName}"/>
     </apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
     <apex:commandButton action="{!save}" value="Save"/>
     </apex:pageBlockButtons>
    </apex:pageBlock>

   </apex:form>

</apex:page>
 
public class ContactSaveExtension {
    
    Contact newCon;

    public ContactSaveExtension(ApexPages.StandardController con) {
        this.newCon = (Contact)con.getRecord();
    }
    
     public pagereference save(){
     
      Account a = New Account(Name = newCon.FirstName + ' ' + newCon.LastName);
      Insert a;
      newCon.AccountID = a.id;
      insert newCon;
     
      Pagereference pr = New PageReference('/apex/Thank_You');
      return pr;

    }

}

Please MARK AS A  BEST ANSWER !!!!!

Regards
Mukesh
Stephanie StoddardStephanie Stoddard
Thank you all for your help!

The next issue I am facing is getting to 75% code coverage to deploy in my production environment (I'm at 63%).  I have not written any test code for this Apex Class.  Is writing test code helpful, and if so, what would be an example?  

​Thank you!