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 

Save Button On My Visual Force Page?

Hello, I would like to have a "Save" button and a "Cancel" button on my visual force page.
The current ones I have dont work because im not using a standard controller, I am using a custom controller instead so that I can pull two different objects. 

What im wondering is can I import the standard controllers save/cancel function into my custom controller or do I have to create my own save & cancel function. If so how? 

Thank You. 

Visual Force Page I would like to add the Save & Cancel buttons to:-
<apex:page lightningStyleSheets="true" Controller="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="{! ApprenticeObj.Name}"/>
            </apex:pageBlockSection>
         </apex:pageBlock>
        
     <!--Apprentice Details Section-->
        <apex:pageBlock title="Apprentice Details">
            <apex:pageBlockSection columns="2">
                <apex:inputField value="{! ApprenticeObj.Apprentice_name__c}"/>
                <apex:inputField value="{! ApprenticeObj.Curriculum_Verified__c}"/>
                <apex:inputField value="{! ApprenticeObj.ULN__c}"/>
                <apex:inputField value="{! ApprenticeObj.Employer__c}"/>
                <apex:inputField value="{! ApprenticeObj.Programme_Area__c}"/>
                <apex:inputField value="{! ApprenticeObj.Vacancy__c}"/>
                <apex:inputField value="{! ApprenticeObj.Learning_Start_Date__c}"/>
                <apex:inputField value="{! ApprenticeObj.Levy_Status__c}"/>
                <apex:inputField value="{! ApprenticeObj.Learner_Reference_Number__c}"/> <!-- Set Programme Agreed with employer -->
                <apex:inputField value="{! ApprenticeObj.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="{! ApprenticeObj.Line_Manager__c}"/>
               <apex:inputField value="{! ApprenticeObj.Line_Manager_Phone__c}"/>
               <apex:inputField value="{! ApprenticeObj.Line_Manager_Email__c}"/>
               <apex:inputField value="{! ApprenticeObj.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="{! ApprenticeObj.English_IA_Result__c}"/> <!-- Set To English Assessment in Live -->
                <apex:inputField value="{! ApprenticeObj.Date_of_Birth__c}"/>
                <apex:inputField value="{! ApprenticeObj.Maths_IA_Result__c}"/> <!-- Set To Maths Assessment in Live -->
                <apex:inputField value="{! ApprenticeObj.Funding_Line__c}"/>
                <apex:inputField value="{! ApprenticeObj.Highest_Previous_Attainment__c}"/>
                <apex:inputField value="{! ApprenticeObj.Highest_ICT_Achievement__c}"/>
                <apex:inputField value="{! ApprenticeObj.Highest_English_Qual__c}"/>
                <apex:inputField value="{! ApprenticeObj.Prior_Learning_Record_Attached__c}"/>
                <apex:inputField value="{! ApprenticeObj.Highest_Maths_Qual__c}"/>
                <apex:inputField value="{! ApprenticeObj.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="{! ApprenticeObj.Apprenticeship_Title__c}"/>
                <apex:inputField value="{! ApprenticeObj.Highest_Maths_Qual__c}"/> <!--Set to Minimum Duration-->
                <apex:inputField value="{! ApprenticeObj.Highest_Maths_Qual__c}"/> <!--Set to Apprenticeship Pathway-->
                <apex:inputField value="{! ApprenticeObj.Highest_Maths_Qual__c}"/> <!--Set to Prefered Duration (Months)-->
                <apex:inputField value="{! ApprenticeObj.Highest_Maths_Qual__c}"/> <!--Set to Apprenticeship Level-->
                <apex:inputField value="{! ApprenticeObj.Development_Coach__c}"/> <!-- May need changing -->
                <apex:inputField value="{! ApprenticeObj.Highest_Maths_Qual__c}"/> <!--Set to Apprenticeship Type-->
                <apex:inputField value="{! ApprenticeObj.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="{! ApprenticeObj.Functional_Maths__c}"/>
                <apex:inputField value="{! ApprenticeObj.Maths_Status__c}"/>
                <apex:inputField value="{! ApprenticeObj.Functional_English__c}"/>
                <apex:inputField value="{! ApprenticeObj.English_Status__c}"/> 
                <apex:inputField value="{! ApprenticeObj.Functional_ICT__c}"/> 
                <apex:inputField value="{! ApprenticeObj.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="{! ApprenticeObj.Functional_Maths__c}"/> <!--Set to Original Start Date-->
                <apex:inputField value="{! ApprenticeObj.Maths_Status__c}"/> <!--Set to Aditional skill/Check if its the same field as below-->
            </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="{! ApprenticeObj.Functional_Maths__c}"/>
                <apex:inputField value="{! ApprenticeObj.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.Course_Name__c}"/> 
                <apex:inputField value="{! ComponentsObj.Course_Name__c}"/> <!--Set to Learning Aim Code-->
                <apex:inputField value="{! ComponentsObj.Course_Name__c}"/> <!--Set to Awarding Organisation-->
                <apex:inputField value="{! ComponentsObj.Course_Name__c}"/> <!--Set to Delivery Location-->
                <apex:inputField value="{! ComponentsObj.Start_Date__c}"/>
                <apex:inputField value="{! ComponentsObj.Planned_End_date__c}"/>
                <apex:inputField value="{! ComponentsObj.Course_Name__c}"/> <!--Set to Percentage of aim to be delivered-->
            </apex:pageBlockSection>
        </apex:pageBlock>
      <!--End of Apprenticeship Components-->
        
      <!--Save/Cancel-->
       
        <apex:pageBlock >
        <apex:commandButton action="{!save }" value="Save" />
        <apex:commandButton action="{!cancel }" value="Cancel" />
        </apex:pageBlock>
        
      <!--End of Save/Cancel-->
            

    </apex:form>
</apex:page>
My Apex Controller Class :- 
public class myController{
public Apprentice__c ApprenticeObj{get;set;}
public Apprenticeship_Component__c ComponentsObj{get;set;}
public Programme_Area__c ProgramArea {get;set;}
public myController(){
         ApprenticeObj = new Apprentice__c();
         ComponentsObj = new Apprenticeship_Component__c();
         ProgramArea = new Programme_Area__c ();
    }
    public void saveObjects(){
         ProgramArea.PA_Code__c = 'P12';
         insert ProgramArea;
         ApprenticeObj.Programme_area__c = ProgramArea.Id;
         ApprenticeObj.ULN__c = '56464646';         
         insert ApprenticeObj;
         ComponentsObj.Apprentice__c = ApprenticeObj.Id;
         insert ComponentsObj;
    }
}

When save is clicked the error I get :-

The Save and Cancel buttonError I get
Best Answer chosen by Daniel Bleasdale
Ajay K DubediAjay K Dubedi
Hi Daniel,

No, you have already created a function saveObjects().
Now, You can use this in replace of save button which is given below.
 
<!--Save/Cancel-->

<apex:pageBlock >
        <apex:commandButton action="{!saveObjects}" value="Save" />
        <apex:commandButton action="{!cancel }" value="Cancel" />
</apex:pageBlock>

<!--End of Save/Cancel-->

Please let me know if you have any query.
Please mark it as best Answer if you find it helpful.

Thank You
Ajay Dubedi

All Answers

Ajay K DubediAjay K Dubedi
Hi Daniel,

If you are using save method in your custom-controller then you can not use standard save button. 
You need to use method name in save button in place of action value.

<apex:pageBlock >
        <apex:commandButton action="{!saveObjects }" value="Save" />
        <apex:commandButton action="{!cancel }" value="Cancel" />
</apex:pageBlock>

Refer this url:-
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_custom.htm

http://www.salesforcetutorial.com/custom-controllers-controller-extensions/

Please let me know if you have any query.
Please mark it as best Answer if you find it helpful.

Thank You
Ajay Dubedi
Ajay K DubediAjay K Dubedi
Hi Daniel,

No, you have already created a function saveObjects().
Now, You can use this in replace of save button which is given below.
 
<!--Save/Cancel-->

<apex:pageBlock >
        <apex:commandButton action="{!saveObjects}" value="Save" />
        <apex:commandButton action="{!cancel }" value="Cancel" />
</apex:pageBlock>

<!--End of Save/Cancel-->

Please let me know if you have any query.
Please mark it as best Answer if you find it helpful.

Thank You
Ajay Dubedi
This was selected as the best answer