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
Daniel BleasdaleDaniel Bleasdale 

How to Use my controller Extention

How do I use my extension "myController" to show information on my visual force page?
User-added image
Im wanting to pull the information up onto this screen which is my VisualForce page :-
User-added image
Here is the code for my VisualForce page:-
<apex:page lightningStyleSheets="true" StandardController="Apprentice__c" extensions="myController" title="Apprentice Documentation">
    <apex:form >
    <!--myController refers to the Apex class I created called myController.apxc this is so I can use two object types in one form in place of a Standard controller
        For refrence ApprenticeObj = new Apprentice__c and ComponentsObj = new Apprenticeship_Component__c-->
        
    <!--Style-->
        <style>
        body{
        font-family: Arial,Helvetica,sans-serif;
        color: #000000;
        Padding:25px;
        }
        </style>
    <!--End of Style-->
        
        <apex:pageBlock >
            <apex:pageBlockSection columns="3">
                <apex:inputField value="{! Apprentice__c.Name}"/>
            </apex:pageBlockSection>
         </apex:pageBlock>
        
     <!--Apprentice Details Section-->
        <apex:pageBlock title="Apprentice Details">
            <apex:pageBlockSection columns="2">
                <apex:inputField value="{! Apprentice__c.Apprentice_name__c}"/>
                <apex:inputField value="{! Apprentice__c.Curriculum_Verified__c}"/>
                <apex:inputField value="{! Apprentice__c.Employer__c}"/>
                <apex:inputField value="{! Apprentice__c.Programme_Area__c}"/>
                <apex:inputField value="{! Apprentice__c.Vacancy__c}"/>
                <apex:inputField value="{! Apprentice__c.Learning_Start_Date__c}"/>
                <apex:inputField value="{! Apprentice__c.Levy_Status__c}"/>
                <apex:inputField value="{! Apprentice__c.Programme_Agreed_With_Employer__c}"/>
                <apex:inputField value="{! Apprentice__c.Existing_Employee__c}"/>
            </apex:pageBlockSection>
         </apex:pageBlock>
        <!--End of Apprentice Details Section-->
        
        <!--Line Manager Details Section-->
         <apex:pageBlock title="Line Manager Details">
            <apex:pageBlockSection columns="2">
               <apex:inputField value="{! Apprentice__c.Line_Manager__c}"/>
               <apex:inputField value="{! Apprentice__c.Line_Manager_Phone__c}"/>
               <apex:inputField value="{! Apprentice__c.Line_Manager_Email__c}"/>
               <apex:inputField value="{! Apprentice__c.Line_Manager_Mobile__c}"/>
            </apex:pageBlockSection>
         </apex:pageBlock>
     <!--End ofLine Manager Details Section-->
        
     <!--Previous Education & Diagnostics-->
       <apex:pageBlock title="Previous Education & Diagnostics">
            <apex:pageBlockSection columns="2">
                <apex:inputField value="{! Apprentice__c.English_Assessment__c}"/>
                <apex:inputField value="{! Apprentice__c.Date_of_Birth__c}"/>
                <apex:inputField value="{! Apprentice__c.Maths_Assessment__c}"/>
                <apex:inputField value="{! Apprentice__c.Funding_Line__c}"/>
                <apex:inputField value="{! Apprentice__c.Highest_Previous_Attainment__c}"/>
                <apex:inputField value="{! Apprentice__c.Highest_ICT_Achievement__c}"/>
                <apex:inputField value="{! Apprentice__c.Highest_English_Qual__c}"/>
                <apex:inputField value="{! Apprentice__c.Prior_Learning_Record_Attached__c}"/>
                <apex:inputField value="{! Apprentice__c.Highest_Maths_Qual__c}"/>
                <apex:inputField value="{! Apprentice__c.Learning_Difficulties__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
      <!--End of Education & Diagnostics-->
        
        <!--Apprenticeship Programme-->
        
        <apex:pageBlock title="Apprenticeship Programme">
            <apex:pageBlockSection columns="2" >
                <apex:inputField value="{! Apprentice__c.Apprenticeship_Title__c}"/>
                <apex:inputField value="{! Apprentice__c.Minimum_Duration__c}"/>
                <apex:inputField value="{! Apprentice__c.Apprenticeship_Pathway__c}"/>
                <apex:inputField value="{! Apprentice__c.Prefered_Duration__c}"/>
                <apex:inputField value="{! Apprentice__c.Apprenticeship_Level__c}"/>
                <apex:inputField value="{! Apprentice__c.Development_Coach__c}"/>
                <apex:inputField value="{! Apprentice__c.Apprenticeship_Type__c}"/>
                <apex:inputField value="{! Apprentice__c.Usual_College_Day__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
        
      <!--End of Apprenticeship Programme-->
        
      <!--Functional Skills-->
        <apex:pageBlock title="Functional Skills">
            <apex:pageBlockSection columns="2" >
                <apex:inputField value="{! Apprentice__c.Functional_Maths__c}"/>
                <apex:inputField value="{! Apprentice__c.Maths_Status__c}"/>
                <apex:inputField value="{! Apprentice__c.Functional_English__c}"/>
                <apex:inputField value="{! Apprentice__c.English_Status__c}"/> 
                <apex:inputField value="{! Apprentice__c.Functional_ICT__c}"/> 
                <apex:inputField value="{! Apprentice__c.ICT_Status__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
      <!--End of Functional Skills-->
        
        <!--Existing Employer Details-->
        <apex:pageBlock title="Existing Employer Details">
            <apex:pageBlockSection columns="2" >
                <apex:inputField value="{! Apprentice__c.Original_Start_Date__c}"/>
                <apex:inputField value="{! Apprentice__c.Additional_Skills_Existing_Employee__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
      <!--End of Existing Employer Details-->
        
        <!--End Point Assessment (EPA)-->
        <apex:pageBlock title="End Point Assessment (EPA)">
            <apex:pageBlockSection columns="2" >
                <apex:inputField value="{! Apprentice__c.Functional_Maths__c}"/>
                <apex:inputField value="{! Apprentice__c.Maths_Status__c}"/>
                <apex:inputField value="{! ComponentsObj.Qual_Aim_Text__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
      <!--End of End Point Assessment (EPA)-->
        
      <!--Apprenticeship Components-->
        
        <apex:pageBlock title="Apprenticeship Components">
            <apex:pageBlockSection columns="1" >
                <apex:inputField value="{! ComponentsObj.Apprenticeship_Qualifications__c}"/> 
                <apex:inputField value="{! ComponentsObj.Learning_Aim_Code__c}"/>
                <apex:inputField value="{! ComponentsObj.Level__c}"/>
                <apex:inputField value="{! ComponentsObj.Delivery_Location__c}"/>
                <apex:inputField value="{! ComponentsObj.Start_Date__c}"/>
                <apex:inputField value="{! ComponentsObj.Expected_End_date__c}"/>
                <apex:inputField value="{! ComponentsObj.Percentage_of_Aim_to_be_Delivered__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
      <!--End of Apprenticeship Components-->
        
      <!--Save/Cancel-->
       
        <apex:pageBlock >
        <apex:commandButton action="{!save }" value="Save" />
        </apex:pageBlock>
        
      <!--End of Save/Cancel-->
            

    </apex:form>
</apex:page>
And here is my Controller Extension 
public class myController{
public Apprentice__c ApprenticeObj{get;set;}
public Apprenticeship_Component__c ComponentsObj{get;set;}
public Programme_Area__c ProgramArea {get;set;}
public Apprenticeship_Qual__c Qual {get;set;}  
ApexPages.StandardController sc;
public myController(ApexPages.StandardController sc){ 
    this.sc = sc; 
    ApprenticeObj = new Apprentice__c(); 
    ComponentsObj = new Apprenticeship_Component__c(); 
    ProgramArea = new Programme_Area__c ();
    Qual = new Apprenticeship_Qual__c ();
}
    public void saveObjects(){
         ProgramArea.PA_Code__c = 'P12';
         insert ProgramArea;
         ApprenticeObj.Programme_area__c = ProgramArea.Id;
         ApprenticeObj.Date_of_Birth__c = date.parse('12/08/2000');
         insert ApprenticeObj;
         ComponentsObj.Apprentice__c = ApprenticeObj.Id;
         Qual.Course_Code__c = '5363hdg';
         Qual.Qual_Aim__c = 'QualAim';
         Qual.Qualification_Type__c = 'Competency';
         insert Qual;
         ComponentsObj.Apprenticeship_Qualifications__c = Qual.Id;
         insert ComponentsObj;
    }
}


Any help is much appreciated thanks
 
Prem RPrem R
Hi, 

Are you unable to save this code?  

 
Ajay K DubediAjay K Dubedi
Hi Daniel,

Please check below url learn about controller and extensions.

Controller Extention in salesforce is an apex class containing a constructor that is used to add or 
extends the functionalities of a standard controller or custom controller in salesforce.
Extention controller is also used to leverage the functionality of another controller using our own custom logic.
controller extensions in Salesforce can be used without a standard controller or custom controller.

And Please check this url also I think this will help you to understand controller and extensions.

URL:https://www.tutorialkart.com/visualforce/controller-extension-in-salesforce/
    
Please let me know if you have any query.
Please mark it as best Answer if you find it helpful.

Thank You
Ajay Dubedi
Shubham NandwanaShubham Nandwana
Hi Daniel,
I'm sorry but the screenshots you added are not clear but I assume you are not able to get values from the extension on your visualforce page.
As with all controller methods, controller extension methods and variables can be referenced with {! } notation in the page markup.
You have to set values in methods/variables using get/set methods.
See below example:
<apex:page standardController="Account" extensions="myControllerExtension">
    {!greeting} <p/>
Hi {!name}
    <apex:form>
        <apex:inputField value="{!account.name}"/> <p/>
        <apex:commandButton value="Save" action="{!save}"/>
    </apex:form>
</apex:page>
 
public class myControllerExtension {

    private final Account acct;
    public String name{get;set;}
    // The extension constructor initializes the private member
    // variable acct by using the getRecord method from the standard
    // controller.
    public myControllerExtension(ApexPages.StandardController stdController) {
        name='Barack';
        this.acct = (Account)stdController.getRecord();
    }

    public String getGreeting() {
        return 'Hello ' + acct.name + ' (' + acct.id + ')';
    }
}

Choose it as best answer if it helps.

Shubham Nandwana.
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts