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
Himanshu GhateHimanshu Ghate 

Visualforce page -->when I am trying to insert the record ,it should take to the details page.

Scenario --> I have created student VF page in which all the fields are there,So when I have inserted Id on the URL ,it's take you to the studnet record details page .when you are inserting any previous student record Id on the url of that VF page. 
But the Problem is--> As an user when the user is trying to insert or make student record on it own or new ,after hitting on the save button ,it's not taking to the studnet record details page .
Please can some one help me.

Image:-
VisualforcePage Error


Apex Class code-->
public class CustContStudentVfEx1ApexClass {
  
        //Now for the custom object
        //Create the visualforce Page when the user will insert the data ,it should be store somewhere so,for that we need to create
        //Variable to insert the value.
    public Student__c stud{get;set;}
     
    //Now to create the constructor when the user will insert the Id on the url we should be giving the Student Details record on the visualforce Page
    public CustContStudentVfEx1ApexClass(){
            
           //When the User will insert the Id on the url ,the particular record should be get display automatically
           ID  id=ApexPages.currentPage().getParameters().get('id');
           //Now to fetch the fields when the user will put the id on the url it should be showing some particular field on  the vf page.
           //But I want to check the user insert the url id is null or not .
           //using the conditional statement and put it inside the stud variable.
        stud=(id==null)?new Student__c():[SELECT Id,Name,State__c,Subject__c,Population__c,Gender__c,D_O_B__c,Age__c,City__c,Class__c FROM Student__c WHERE ID=:id];
            
 
    }
    //Now when the user will hit on the save button ,the record should be get shown update or new one
    public PageReference Save(){
        
             //To check the insert record is giving an error or not 
        try{
                
            upsert(stud);
        }
        catch(System.DmlException e){             
  
              //Now if an error occur I need to show this error on the vf to show an error
              ApexPages.addMessages(e);
              return null; //Now to return to the Page where we are inserting the record or fill up 
           
              
        }
        //Now ,if the error doesn't occur ,then take me to the details page of the records
        PageReference  studentDetailsPage=new ApexPages.StandardController(stud).view();
        return studentDetailsPage;
        
    }
    //For the cancel the it will take you to the current vf page
    public Pagereference Cancel(){
         
         return null;
    }
}

VisualforcePage code-->
<apex:page controller="CustContStudentVfEx1ApexClass" lightningStylesheets="true">
<apex:form >
<apex:pageBlock title="Student">
<apex:pageBlockSection title="StudentInformation"  collapsible="true" columns="2"> 

<apex:inputField value="{!stud.Name}"/>
<apex:inputField value="{!stud.State__c}"/>
<apex:inputField value="{!stud.Subject__c}"/>
<apex:inputField value="{!stud.Population__c}"/>
<apex:inputField value="{!stud.Gender__c}"/>
<apex:inputField value="{!stud.D_O_B__c}"/>
<apex:inputField value="{!stud.Age__c}"/>
<apex:inputField value="{!stud.City__c}"/>
<apex:inputField value="{!stud.Class__c}"/>\
</apex:pageBlockSection>
<apex:pageBlockButtons location="Bottom">

<apex:commandButton value="Save" action="{!Save}"/>
<apex:commandButton value="Cancel" action="{!Cancel}"/>
</apex:pageBlockButtons>

</apex:pageBlock>

</apex:form>
</apex:page>
Best Answer chosen by Himanshu Ghate
AshwiniAshwini (Salesforce Developers) 
Hi Himanshu,
Issue seems to be with save method. The problem is that when you save a new student, the system doesn't know the student's details page yet because the new student doesn't have an ID until it's saved. 

To fix this, you  will need to make changes in the logic such that ,If it's an existing student, it should take you to the details page right away. If it's a new student, it should create a details page for the new student.

 You can refer below sample code for your reference:
public PageReference Save(){
    try {
        upsert stud;
    } catch (System.DmlException e) {
        ApexPages.addMessages(e);
        return null;
    }
    
    PageReference studentDetailsPage;
    
    if (stud.Id != null) {
        studentDetailsPage = new PageReference('/' + stud.Id);
    } else {
        studentDetailsPage = new PageReference('/' + Page.StudentDetailPage);                                  // Replace 'StudentDetailPage' with the actual name of your student details page.
    }
    
    return studentDetailsPage;
}
If this information helps, please mark the answer as best. Thank you