Play Games

Search This Blog

Tuesday, January 5, 2016

How to call apex method from command button in visualforce page


Apex Class:
public class CallApexMethodFromVF {
    public Account acc {get;set;}
    public CallApexMethodFromVF () {
        acc = new Account ();
    }
    public pagereference saveData() {
        insert acc;
        PageReference accountDetailPage = new ApexPages.StandardController(acc).view();
        accountDetailPage.setRedirect(true);
        return accountDetailPage;
    }
}
Visualforce Page :
<apex:page Controller="CallApexMethodFromVF">
    <apex:form >
        <apex:pageBlock title="Account Information" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!saveData}" value="Save Record"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="My Content Section" columns="2">
                <apex:inputField value="{!acc.name}"/>
                <apex:inputField value="{!acc.site}"/>
                <apex:inputField value="{!acc.type}"/>
                <apex:inputField value="{!acc.accountNumber}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Output :
 

No comments:

Post a Comment