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

How to Create Surrogate SObjects

In a previous post Doug Chasman wrote:
... and sometimes you want to combine the 2 concepts, leverage the cool automagic apex:inputField picklist (or date picker is another common request) behavior when you don't have an Sobject. For that you just create a surrogate or proxy, in memory only, SObject instance that you can bind to - basically using the SObject purely as a data transfer object.
How do I create a surrogate in memory only Sobject? I've looked through the Apex and VF documentation but can't see anything realted to this. I would think I need to define an object with the relavent fields I want to use in the VF page but I don't see how to associate the object as a SObject or specify the various field attributes (like required).
You might consider just creating an internal Apex class on the fly and bind it to your component.
I am sure if this will fulfill your requirements but i guess i am thinking loudly here.

Ron HessRon Hess
Let's say you want a required field of type string.

first, Identify an object in the system that has such a field, say

then you create an account (in memory) in your controller  like this

 public Account myVirtualObj { get; private set;} { myVirtualObj = new Account(); }

now you can use { ! }  in the Visualforce page, in an inputField tag

then access the value of this in your controller using

   string required_string =;

if i am correct it will also draw the required color ( it may need to be in a page block section for that)
Thanks for the example Ron - that solved most of my problems. The formatting on these fields is correct but I have one field that I can't get formatted with the SF style.
I need a list box that I have prepopulated with some account names. The user has to select one of the options on the list. I have accomplished this with the following VF code:
            <apex:outputLabel value="Account Name" for="accountName"/>
              <apex:selectList id="accountName" required="true" size="1" value="{!selectedAccountID}"> 
                  <apex:selectOptions value="{!leadAccountNames}"/>
So is there a way to accomplish this using an existing SF field. If yes how do I hook in to set the options in the select list?
or is there another way? (using SF css styles??)