+ Start a Discussion
Patricia LimPatricia Lim 

How to add save button in VF page with custom controller

Hi all. I have a VF page with a custom controller that saves new records from 2 objects in the same single page. I need a save button for this page and it is returning the error 
Unknown method 'c2.save()'
I suspect this is because I am using a custom controller instead of a standard one - how can I configure my controller so that I can add the save button, and any other buttons?

Apex class:
public class c2{
    public scholarship_award__c sch {get;set;}
    public recipient__c rec {get;set;}
    
    public c2(){
        sch = new scholarship_award__c();
        rec = new recipient__c();
    }
    
    public void saveScholarship(){
        insert rec;
        rec = new recipient__c();
        
        insert sch;
        sch = new scholarship_award__c(Recipient__c=rec.Name + rec.Last_Name__c);
    }
}

VF page:
<apex:page controller="c2">
	<apex:form >
        <apex:pageBlock title="Academic Year 2020-2021">
        <apex:pageBlockSection title="Add Scholarships">
            <apex:inputField value="{!rec.Name}"/>
            <apex:inputField value="{!rec.Last_Name__c}"/>
            <apex:inputField value="{!rec.Preferred_Name__c}"/>
            <apex:inputField value="{!sch.School__c}"/>
            <apex:inputField value="{!sch.Year__c}"/>
            <apex:inputField value="{!sch.Award__c}"/>
            <apex:inputField value="{!rec.Email__c}"/>
            <apex:inputField value="{!rec.Specialty__c}"/>
            <apex:inputField value="{!rec.Biography__c}"/>
        </apex:pageBlockSection>
        <apex:pageBlockButtons >
        	<apex:commandButton action="{!save}" value="Save"/>
        </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Thanks!
 
Best Answer chosen by Patricia Lim
ShirishaShirisha (Salesforce Developers) 
Hi Patricia,

Greetings!

Yes,you can create the method and use it on the button as action.

If you have the method "saveScholarship()" which performs the Save action then you can call it on the CommandButton as below:
 
<apex:commandButton action="{!saveScholarship}" value="Save"/>

Reference:http://himanshurana.in/visualforce-custom-save-and-cancel-button/

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri