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
seahorcesolnsseahorcesolns 

Displaying Sobject Fields in VF with Custom Settings

I have a VF page that I am using custom settings to return the field names of sobjects.  Now I want to use those field names as input fields on the page to allow users to enter values in them so that I can filter my data by those values.  Is this possible?

 

Right now I'm just getting the names, but can't figure out what method to use to get a field as an input.

 

Any ideas would be appreciated.

aballardaballard

Sounds like you need to use a synamic reference to combine the field name with the object....

 

http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#StartTopic=Content/pages_dynamic_vf.htm

Nathan B.ax1173Nathan B.ax1173

We're aware of how to associate an object/field with a apex:input tag, but we haven't found a way to programatically, and dynamically, set the value attribute to a dynamic value.  Salesforce expects an actual object/field to be present in the value attribute of the inputField vs string entry.

 

I've provided an example below that I hope will be helpful in explaining what we're trying to achieve.

 

Non-Functional Example:

 

Controller Code

Map<String, List<String>> objectsToFields = new Map<String, List<String>>();

objectsToFields.put("My_Test_Object__c", new List<>());
objectsToFields.get("My_Test_Object__c").add("Name");
objectsToFields.get("My_Test_Object__c").add("Description__c");

 

VF Page

<apex:repeat value="{!objectsToFields.keySet()}" var="obj">
     <apex:repeat value="{!objectsToFields.get(obj)}" var="fld">
          <apex:inputField value="{!obj.fld}"/>
     </apex:repeat>
</apex:repeat>

 

We've also tried using an sobject map with a list of string field names as the values to potentially use sobject.get('fieldname'), but that hasn't worked either.

aballardaballard
<apex:repeat value="{!objectsToFields.keySet()}" var="obj">
     <apex:repeat value="{!objectsToFields.get(obj)}" var="fld">
          <apex:inputField value="{!obj[fld]}"/>
     </apex:repeat>
</apex:repeat>

 (See the documentation I linked in my previous reply)

aballardaballard

(Oops, that won't wuite work because you are using strings for object names also... I think you will have to have your controller reeturn the actual objects to be displayed.... just a name does not make sense)

 

Nathan B.ax1173Nathan B.ax1173

Right, essentially the below, but SF wants to receive an actual object/field vs a string value for the object/field.

 

<apex:repeat value="{!objectsToFields.keySet()}" var="obj">
     <apex:repeat value="{!objectsToFields.get(obj)}" var="fld">
          <apex:inputField value="{!obj.obj[fld]}"/>
     </apex:repeat>
</apex:repeat>

 

We've also tried a Map of sObjects with a List of Strings for fields. (Map<sObject, List<String>>), but we haven't found a way to pull the field directly from the sObject using sObject.get() or some other method.  We also noticed that SF has a pilot program for something called "Dynamic VF", but we don't know enough about that program to say that it would be the solution.

aballardaballard

Dynamic references (using subscript notation) are available now.   I don't see why that doesn't meet your requirement. 

 

Nathan B.ax1173Nathan B.ax1173

Upon further review it looks like this is half of the solution.  The issue we have in using this that it is for hard-typed sObjects not for loosely typed sObjects.

 

Ie.

Works for:

VF: {!Account[name]}

OR

VF: {!My_Custom_Obj__c[field_name__c]}

 

Need to determine how to make it work for:

Apex: sObject tmpObj = Schema.SObjectType.newSObject();

VF: {!tmpObj[field_name__c]}

 

 

Forgot to mention that the above Schema.SObjectType.newSObject comes from a Schema.getGlobalDescribe() map.