0

I have a VF page with standard controller Account and few input fields. A user will enter the data in VF page.

Now i want to get these fields to controller. How to achieve this.

<apex:page standardController="Account" 
 extensions="account">
    <apex:form >
        <apex:pageBlock title="Select an Appropriate Action">
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!changeStatus}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection >
            <apex:pageBlockSection rendered="{!IdAndExpirationDateSection}">
                    <apex:inputField value="{!Account.id}" />
                    <apex:inputField value="{!Account.name}" />
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

I want id and name fields data in controller.

2 Answers 2

1

First of all I'd say consider renaming your class. For example, you've named your extension account which can conflict with the Account sObject. I had a simialr problem calling a utility class User. You can read more about that in my question here with a great answer from Adrian Larson.

Secondly, you're trying to create an <apex:inputField> referencing an AccountId which you can't do. Ids are system generated. You can only read them, not write to them.

Because you're using an extension, you'll need to pass in the standard controller as a parameter in the constructor and set it's value to a variable:

public class AccountExtension {
  /**
   * The variable that stores the Account details
   *
   * @var Account
   */

  private final Account record;

  /**
   * Contructor. Takes a standard controller variable and assigns it to the record
   *
   * @param ApexPages.StandardController stdController
   */

  public AccountExtension(ApexPages.StandardController stdController) {
    this.record = (Account)stdController.getRecord();
  }
}

You'll now be able to access fields in your Visualforce Page using typical dot notation.

For example:

<apex:page showHeader="true" sidebar="true" standardController="Account" extensions="AccountExtension">
  <apex:form >
    <apex:pageBlock id="container" title="Account Form">
      <apex:pageBlockButtons>
        <apex:commandButton action="{!Save}" value="Save" reRender="errors" status="loading" />
        <apex:commandButton action="{!Cancel}" value="Cancel" reRender="errors" status="loading" />
        <apex:actionStatus id="loading">
          <apex:facet name="start">
            <img src="/img/loading.gif" />
          </apex:facet>
        </apex:actionStatus>
      </apex:pageBlockButtons>
      <apex:pageBlockSection title="Information">
        <apex:inputField value="{!Account.Name}" />
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>

If you want to access posted values in your extension, you can simply reference the record variable. For example, if you created a Save() method that would override the standard Save() method in your extension:

public PageReference Save() {
  String accountName = record.Name;
}

Or, as per your markup you're using a method called changeStatus:

public PageReference changeStatus() {
   String accountName = record.Name;
}
0

You can't populate Id field manually, it is system generated and as far as name is concerned just declare account variable in your controller:

public Account act{get;set;}

public void changeStatus() {
   System.debug('account name:'+act.Name);
}
3
  • Thanks for your Response Rohit . I tried your code, But it throw an error. Attempt to de-reference a null object
    – venkatesh
    Commented Jul 14, 2017 at 8:56
  • I want to send this values to existing record
    – venkatesh
    Commented Jul 14, 2017 at 9:07
  • @venkatesh take a look at standard controller with extension example. Commented Jul 14, 2017 at 9:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .