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
DaveHagmanDaveHagman 

Unknown error with visualforce markup

Hi All,

 

I am new to VisualForce and I am still going through some testing with it to get more familiar. I am making a simple page where the user will enter a contact name and a list of all contacts that match the criteria will be displayed (in a dataTable). I setup my custom controller and wrote some VF markup for the page but I am getting the following error: 
Error: Could not resolve the entity from <apex:inputField> value binding '{!accountName}'. inputField can only be used with SObject fields.

 

I'm not sure what this means. My sample code is below. Does anyone have any suggestions?

 

 

Custom Controller

 

public class CardholderVfController {
    private String accountName;
    private List<String> accountList;
    private String contactName;
    private List<String> contactList;
    private final Case c;  
    
    //public CardholderVfController(ApexPages.StandardController stdController)
    //{
        //this.c = (Case)stdController.getRecord();
        //accountList = new List<String>(new String[] {'Test1', 'Test2'});
    //}
    
    public List<String> getAccounts()
    {
        accountList = new List<String>(new String[] {'TestAccountText1', 'TestAccountText2'});
        return this.accountList;
    }   
    
    public List<String> getContacts()
    {
        return this.contactList;
    }
    
    public void setAccountName(String s)
    {
        accountName = s;
    }
    
    public String getAccountName()
    {
        return this.accountName;
    }
    
    

}

 Page Markup

 

<apex:page controller="CardholderVfController">
<apex:form >
    <apex:pageBlock > 
    <apex:pageBlockSection >
        <apex:inputField id="accountNameField" value="{!accountName}"/>
        <apex:commandButton id="findAccount" title="Search"/>
    </apex:pageBlockSection>
    <apex:pageBlockSection >
            <apex:dataTable align="center" value="{!accounts}" var="a" >
                <apex:column value="{!a}"/>
            </apex:dataTable> 
    </apex:pageBlockSection>            
    </apex:pageBlock>
    {!accounts[0]}
</apex:form>
</apex:page>

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Anand_agrawal@persistent.co.inAnand_agrawal@persistent.co.in

Hi Dave,

 

I have come across such problems.

 

So whenever you are using the <apex:inputField> on visual force page. It expects the tag to be bound with some object (Custom objects or standard objects) not with the local class variables.

 

So in your code  instead of binding the inputField to accountName, bind it to some object. so here is the code you should use :

 

<apex:page controller="CardholderVfController">
<apex:form >
    <apex:pageBlock > 
    <apex:pageBlockSection >
        <apex:inputField id="accountNameField" value="{!accountObj.Name}"/>
        <apex:commandButton id="findAccount" title="Search"/>
    </apex:pageBlockSection>
    <apex:pageBlockSection >
            <apex:dataTable align="center" value="{!accounts}" var="a" >
                <apex:column value="{!a}"/>
            </apex:dataTable> 
    </apex:pageBlockSection>            
    </apex:pageBlock>
    {!accounts[0]}
</apex:form>
</apex:page>
public class CardholderVfController {
    private String accountName;
    private List<String> accountList;
    private String contactName;
    private List<String> contactList;    
    public Account accountObj{get;set;}
private final Case c;
public CardholderVfController(){
accountObj = new Account(Name='Account1');
}


 //public CardholderVfController(ApexPages.StandardController stdController) //{ //this.c = (Case)stdController.getRecord(); //accountList = new List<String>(new String[] {'Test1', 'Test2'}); //} public List<String> getAccounts() { accountList = new List<String>(new String[] {'TestAccountText1', 'TestAccountText2'}); return this.accountList; } public List<String> getContacts() { return this.contactList; } public void setAccountName(String s) { accountName = s; } public String getAccountName() { return this.accountName; }}

All Answers

Anand_agrawal@persistent.co.inAnand_agrawal@persistent.co.in

Hi Dave,

 

I have come across such problems.

 

So whenever you are using the <apex:inputField> on visual force page. It expects the tag to be bound with some object (Custom objects or standard objects) not with the local class variables.

 

So in your code  instead of binding the inputField to accountName, bind it to some object. so here is the code you should use :

 

<apex:page controller="CardholderVfController">
<apex:form >
    <apex:pageBlock > 
    <apex:pageBlockSection >
        <apex:inputField id="accountNameField" value="{!accountObj.Name}"/>
        <apex:commandButton id="findAccount" title="Search"/>
    </apex:pageBlockSection>
    <apex:pageBlockSection >
            <apex:dataTable align="center" value="{!accounts}" var="a" >
                <apex:column value="{!a}"/>
            </apex:dataTable> 
    </apex:pageBlockSection>            
    </apex:pageBlock>
    {!accounts[0]}
</apex:form>
</apex:page>
public class CardholderVfController {
    private String accountName;
    private List<String> accountList;
    private String contactName;
    private List<String> contactList;    
    public Account accountObj{get;set;}
private final Case c;
public CardholderVfController(){
accountObj = new Account(Name='Account1');
}


 //public CardholderVfController(ApexPages.StandardController stdController) //{ //this.c = (Case)stdController.getRecord(); //accountList = new List<String>(new String[] {'Test1', 'Test2'}); //} public List<String> getAccounts() { accountList = new List<String>(new String[] {'TestAccountText1', 'TestAccountText2'}); return this.accountList; } public List<String> getContacts() { return this.contactList; } public void setAccountName(String s) { accountName = s; } public String getAccountName() { return this.accountName; }}
This was selected as the best answer
sfdc guy.ax723sfdc guy.ax723

Hi Dave,

 

The <apex:inputField /> must reference an sObject meaning a Salesforce.com object not a propoerty of your class. What you would want to use is

<apex:inputText id="accountNameField" value="{!accountName}"/>

 

To use inputField you would have to use the following: <apex:inputField id="accountNameField" value="{!account.Name}"/>

This would return the Name of the Account referenced by your standard Controller.

 

Hope that helps?

 

sfdc guy

DaveHagmanDaveHagman

That did the trick! Thanks a lot guys.