To use apex class as controller in salesforce

Visualforce:

<apex:page controller="myController" tabStyle="Account">
  <apex:sectionHeader title="New Account Edit Page" />
  <apex:form>
    <apex:pageBlock title="Create a New Account">
      <apex:pageBlockButtons location="bottom">
        <apex:commandButton action="{!sav}" value="Save"/>
      </apex:pageBlockButtons>
      <apex:pageBlockSection title="Account Information">
        <apex:inputField id="accountName" value="{!account.name}"/>
        <apex:inputField id="accountSite" value="{!account.site}"/>
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>


Apex:

public class myController 
{
   Account account;

   public Account getAccount() 
   {
      if(account == null) account = new Account();
      return account;
   }

   public PageReference sav() 
   {
      insert account;
    
      PageReference acctPage = new ApexPages.StandardController(account).view();
      acctPage.setRedirect(true);
      return acctPage;
   }


Cheers!!!

No comments:

Post a Comment