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
SDFC FirstLevelerSDFC FirstLeveler 

Help to create custom Edit Button

Hello Experts,

I am new to this, I've two objects one is StudentRecord__c and other one is TechCourse__c. The TechCourse__c is parent of the StudentRecord__c(lookup).

I just wanted to perform custom update through the update button, but getting an error :
"System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: StudentRecord__c.TechCourse__c"

Please review my code and help me. 
VF Page (StudentInsert):
<apex:page standardController="StudentRecord__c" extensions="StudentRecord">
  <apex:form >
  <apex:pageBlock title="Record A Student"> 
  <apex:PageBlockSection Title="Basic Details">
      <apex:inputField value="{!Student.TechCourse__c}"/> <!--TechCourse__c is custom lookup field in StudentRecord__c-->
      <apex:inputField value="{!Student.StudentName__c}"/>
      <apex:inputField value="{!Student.ParentName__c}"/>
      <apex:inputField value="{!Student.DOB__c}"/>
      <apex:inputField value="{!Student.Gender__c}"/>
  </apex:PageBlockSection>
  
  <apex:PageBlockSection Title="Contact Details">
      <apex:inputField value="{!Student.ContactNo__c}"/>
      <apex:inputField value="{!Student.EmailID__c}"/>
      <apex:inputField value="{!Student.Address__c}"/>
  </apex:PageBlockSection>
  
  <apex:pageBlockButtons >
      <apex:commandButton value="Save Student" action="{!saveStudent}"/>
      <apex:commandButton value="Save and New"/>
      <apex:commandButton Value="Cancel" action="{!cancel}"/>
  </apex:pageBlockButtons>
  
  </apex:pageBlock>
  
  </apex:form>
</apex:page>
VF PAGE: ViewStudent
<apex:page StandardController="StudentRecord__c" extensions="StudentRecord" action="{!viewStudent}">
  <apex:form >
          <apex:pageBlock title="Record A Student"> 
          <apex:PageBlockSection Title="Basic Details">
              
              <apex:outputField value="{!Student.TechCourse__c}"/>
              
              <apex:outputField value="{!Student.StudentName__c}"/>
              <apex:outputField value="{!Student.ParentName__c}"/>
              <apex:outputField value="{!Student.DOB__c}"/>
              <apex:outputField value="{!Student.Gender__c}"/>
          </apex:PageBlockSection>
          
          <apex:PageBlockSection Title="Contact Details">
              <apex:outputField value="{!Student.ContactNo__c}"/>
              <apex:outputField value="{!Student.EmailID__c}"/>
              <apex:outputField value="{!Student.Address__c}"/>
          </apex:PageBlockSection>
                                     
          <apex:pageBlockButtons > 
                    <apex:commandButton Value="Update" action="{!EditStudent}"/>
            </apex:pageBlockButtons>   
                                                   
      </apex:pageBlock>
    </apex:form> 
</apex:page>

Controller (StudentRecord):
public class StudentRecord {
    
    public StudentRecord__c Student{set;get;}

    public StudentRecord(ApexPages.StandardController controller) {
        Student= (StudentRecord__c)controller.getRecord();    

    }
    
    public PageReference saveStudent(){
         
         insert Student;
         
         pageReference StuView = new PageReference(Page.ViewStudent.getUrl()+'?Id='+Student.Id);
         StuView.setRedirect(true);
         return StuView;
 
    }       
 public void viewStudent(){
        String id = ApexPages.currentPage().getParameters().get('Id');
        if(id != null){
            Student= [select Id, TechCourse__c, StudentName__c, ParentName__c, DOB__c, Gender__c, ContactNo__c, EmailID__c, Address__c from StudentRecord__c 
            where Id = :id];
              }
    } 
    
 //this is the function i want to run 
public PageReference EditStudent(){
        String id = ApexPages.currentPage().getParameters().get('Id');
        if(id != null){
            Student= [select Id, TechCourse__c, StudentName__c, ParentName__c, DOB__c, Gender__c, ContactNo__c, EmailID__c, Address__c from StudentRecord__c 
            where Id = :id];
              }
         else{return null;}
         pageReference StuView = new PageReference(Page.StudentInsert.getUrl()+'?Id='+Student.Id);
         StuView.setRedirect(true);
         return StuView;
         
    } 
 


}

Thanks in advance.
 
 
D-CoderD-Coder
In VF PAGE: ViewStudent - Line 6
Try this :
<apex:outputField value="{!Student.TechCourse__r.Name}"/>

Update SOQL Query at both the places as :

Student= [select Id, TechCourse__c, TechCourse__r.Name, StudentName__c, ParentName__c, DOB__c, Gender__c, ContactNo__c, EmailID__c, Address__c from StudentRecord__c  where Id = :id];