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
umair ayazumair ayaz 

A visualforce page that allows to create a new account and display all accounts in a table

Hi I am a newbie to visualforce. I am trying to create a visualforce page that displays all the accounts in a table and a form that allows to save accounts. I have written coding but facing issues.

Following is my visualpage sourcecodes.
<apex:page controller="AddAccountController" tabStyle="Account" showHeader="false" sidebar="false">
     <apex:form >
   <apex:pageBlock title="Add Account">
       
       
       <apex:pageBlockSection title="Account Details" columns="1"> 

                <apex:inputField value="{!Account.name}" /> 
                <apex:inputField value="{!Account.site}"/> 
                <apex:inputField value="{!Account.type}"/> 
                <apex:inputField value="{!Account.accountNumber}"/> 
           		<apex:commandButton value="Save" action="{!save}">
                <apex:commandButton value="Cancel" action="#">
                    </apex:commandButton>
           </apex:commandButton>
            </apex:pageBlockSection> 
       
<apex:pageBlockTable value="{!Account}" var="account"> 

      <apex:column value="{!account.Name}"/> 

      <apex:column value="{!account.Site}"/> 

      <apex:column value="{!account.type}"/> 

   </apex:pageBlockTable> 


    </apex:pageBlock>
      </apex:form>
</apex:page>
Here on its my AddAccountController class
public class AddAccountController {

    public List <Account> account=new List <Account>();
    public Account act{get;set;}

    public AddAccountController() {
        account = [SELECT Id, Name, Site FROM Account];                 
    }

    public List <Account> getAccount() {
        return account;
    }

    public PageReference save() {
        upsert (account);
        return null;
    }
}
 
Could not resolve the entity from <apex:inputField> value binding '{!account.name}'. <apex:inputField> can only be used with SObjects, or objects that are Visualforce field component resolvable.
This is the error that i get. Please experts help a newbie :).
 
Best Answer chosen by umair ayaz
Alain CabonAlain Cabon
Hi,

{!account.name} is resolved is there is a standard controller, a getter account returning a single account or a property named account.

Here, there are a list for the getter and the name is act for the property

<apex:inputField value="{!act.name}" />

The following code is what you are trying to do 

Use also:  <apex:pageBlockButtons>  for the block of buttons.
 
<apex:page controller="AddAccountController" tabStyle="Account" showHeader="false" sidebar="false">
    <apex:form >
        <apex:pageBlock title="Add Account">
            <apex:pageBlockButtons>                   
                <apex:commandButton value="Save" action="{!save}" reRender="liste" />
                <apex:commandButton value="Cancel" action="{!cancel}" />
            </apex:pageBlockButtons>  
            <apex:pageBlockSection title="Account Details" columns="1">            
                <apex:inputField value="{!act.name}" /> 
                <apex:inputField value="{!act.site}"/> 
                <apex:inputField value="{!act.type}"/> 
                <apex:inputField value="{!act.accountNumber}"/>              
            </apex:pageBlockSection>          
            <apex:pageBlockTable value="{!Account}" var="acc" id="liste">              
                <apex:column value="{!acc.Name}"/>            
                <apex:column value="{!acc.Site}"/>               
                <apex:column value="{!acc.type}"/>               
            </apex:pageBlockTable>             
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class AddAccountController {
    
    public List <Account> account=new List <Account>();
    public Account act{get;set;}
    
    public AddAccountController() {       
        act = new Account();
    }
    
    public List <Account> getAccount() {
        account = [SELECT Id, Name, Site, Type FROM Account order by createddate desc limit 20];
        return account;
    }
    
    public PageReference cancel() {
        return null;
    }
    
    public PageReference save() {
        system.debug('act name:' + act.name + ' = ' + act.accountNumber);
        upsert act;
        return null;
    }
}
Best regards

Alain