+ Start a Discussion
Rick SF AdminRick SF Admin 

Apex Code to redirect to another "Your profile has been submitted successfully" VF page.

I'm creating a VF page for employees to create a record for a custom object. I'm having issues writing the redirect Apex code so that after they hit "Submit", the employees will be redirected to another VF page (that I've created called "SuccessPage") that says "Your profile has been submitted successfully". After five seconds the SuccessPage it automatically redirect to the company's website. But if the employee hits Cancel, it will close the VF page.

Record Code
<apex:page standardController="Object__c"> 
<apex:form >

<div class="header">
    <center><img src="{!$Resource.companylogo}" width="10%"/></center>
  </div> 
 
        <style>
            body .bPageBlock .pbBody .blue .pbSubheader{
                background-color:#154f82;
            
            }
            body .bPageBlock .pbBody .white .pbSubheader h3{
                color:#ffffff;
            }
        </style>
        

<apex:outputPanel styleClass="blue" layout="block">
      <apex:pageBlockSection title="Skills" id="section7">
       <apex:inputField value="{!Employee_Profile__c.Skills__c}"/>        
</apex:pageBlockSection>
</apex:outputPanel>

</apex:pageBlock>

<center><apex:commandButton value="Submit" action="{!save}"/><apex:commandButton value="Cancel" action="{!cancel}"/></center> 


  </apex:form>
</apex:page>
Success VF page
<apex:page >
    <h1>Success</h1>

  
  <script type="text/javascript">
    window.setTimeout("redirectpage();", 5000);    
    function redirectpage(){
    window.top.location.href = 'http://www.website.com';
    }
</script>
</apex:page>




 
Best Answer chosen by Rick SF Admin
Tad Aalgaard 3Tad Aalgaard 3
Your action is save.

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

But your method is called saveAndSuccess.

What's happening is that when you hit the save button it's calling the default "hidden" save method of the StandardController and ignoring your saveAndSuccess method. 

Change to 

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

Add some System.debug in the saveAndSuccess method so that you can verify via logging that you're hitting that method.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_ApexPages_StandardController_methods.htm

All Answers

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi, Let us know if it helps.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar
 
Rick SF AdminRick SF Admin
The is the Controller (Apex Class) that I am using for the redirect
 
public with sharing class MyPageController {

    Employee_Profile__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.EmployeeProfileSuccessful;
        successPage.setRedirect(true);
        successPage.getParameters().put('id',controller.getId());
        return successPage;
        } return null; 
    }
}

 
Tad Aalgaard 3Tad Aalgaard 3
Your action is save.

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

But your method is called saveAndSuccess.

What's happening is that when you hit the save button it's calling the default "hidden" save method of the StandardController and ignoring your saveAndSuccess method. 

Change to 

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

Add some System.debug in the saveAndSuccess method so that you can verify via logging that you're hitting that method.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_ApexPages_StandardController_methods.htm
This was selected as the best answer