How to redirect to another Vf page after saving a record

To redirect to another page which is saved in your organization, use the standard feature provided by Salesforce PageReference.

A PageReference is a reference to an instantiation of a page.

This example explains the process of redirecting to another VF Page when you save an account record. 

                                          Example

Controller--

public class MyExtension 
{
    public Account ac {get;set;}
    
    public MyExtension(ApexPages.StandardController controller) 
    {
        ac = new Account();       
    }
    
    public PageReference save()
    {
        insert ac;
        PageReference pg = new PageReference('/apex/SecondVFPage');//Second //VFPage is another visualforce page
        pg.setRedirect(true);
        return pg;
    }

}

VF Page--

<apex:page standardController="Account" extensions="MyExtension">
  <apex:form >
      <apex:pageBlock >
          <apex:pageBlockSection >
              <apex:inputField value="{!ac.name}"/>
              <apex:inputField value="{!ac.rating}"/>
          </apex:pageBlockSection>
          
          <apex:commandButton action="{!save}" value="Save"/>
      </apex:pageBlock>
  </apex:form>

</apex:page>

Comments

Popular posts from this blog

How to show or hide a particular section of Visualforce Page dependent upon picklistfield value

Process Automation Specialist Superbadge

Dynamically Add/Delete rows in Visualforce Page