The Apex Developer's Guide section on sObject Types has the following:
In this developer's guide, the term sObject refers to any object that can be stored in the Force.com platform database.
A strategy that I use sometimes is to wrap an sObject in a class to display on a VF page and access in a controller. For example:
public class MyController {
public class AccountWrapper {
public AccountWrapper(Account a) {
acct = a;
selected = false;
}
public Account acct { get; set; }
public Boolean selected { get; set; }
}
public List<AccountWrapper> wrappers { get; set; }
public MyController() {
wrappers = new List<AccountWrapper>();
for (Account a : [Select Id, Name From Account Limit 10]) {
wrappers.add(new AccountWrapper(a));
}
}
}
Then in the VF inside of some form (the apex:outputField causes problems):
<apex:repeat value="{!wrappers}" var="wrapper">
<apex:inputCheckbox value="{!wrapper.selected}"/>:
<apex:outputField value="{!wrapper.acct}"/><br />
</apex:repeat>
Then back in the Contoller some sort of method with DML, such as:
public PageReference saveTheWrappers() {
List<Account> accts = new List<Account>();
for (AccountWrapper wrapper : wrappers) {
if (wrapper.selected) {
accts.add(wrapper.acct);
}
}
update accts;
// or, just to illustrate that acct can be updated while it is wrapped:
update wrappers.get(0).acct;
}
The problem is that in the VF page I get the following error:
Error: Could not resolve the entity from value binding '{!wrapper.acct}'. can only be used with SObject fields.
Why is it that I can save the wrapped acct as an sObject, but I cannot access it as an sObject from the apex:outputField context?
