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
MG ConsultingMG Consulting 

inputField Dynamic Binding?

Hi,

 

Quick question, the answer to which I am guessing is currently no but someday yes:

 

<apex:inputField value="{!Contact.Name}"/> <!-- Static -->

 

<apex:inputField value="{!Contact.get('Name')}"/> <!-- Dynamic -->

 

Is there anyway currently to do something like the Dynamic example above, so that the field my be varied at runtime?

 

It would certainly make my life a whole lot easier... 

 

Thanks a lot,

Mike 

Force2b_MikeForce2b_Mike
What is your main objective?
MG ConsultingMG Consulting
I'm storing the Contact field's API Name in the db (so that it is user configurable) and would like to be able to bind it to an inputField (so that I don't have to build my own support for all possible field types).
Force2b_MikeForce2b_Mike

VisualForce doesn't support the Get(), but what you could do is use Dynamic DML in your Apex so that the query retrieves every field in the object, even custom fields added after the code was deployed. Then your user could edit the VisualForce to display whatever fields they wanted. It's not as slick as controlling the fields in a table, but it does let your user get away with making changes without having to do them in a Sandbox and deploy.

 

Alternatively, you could create a wrapper class for your object that has generic methods for the fields - field1, field2, field3, etc. The Apex could read your customization object to retrieve the field names and then assign them to field1, field2, field3, etc. That would enable the user to completely control what appears in your VF page without them having to actually edit the VF page contents. 

 

Mike 

MG ConsultingMG Consulting

Thanks for the suggestions, unfortunately, I definitely can't have them editing the Visualforce :(

 

But, I'm actually currently working on a design similar to your alternative suggestion.

 

The problem is, you loose all the magic of the inputField, i.e. the way it just displays any field type optimally. For starters, this means that I'm going to have to roll my own date picker, among other not so fun things. It sucks to loose it, but I guess I gotta do what I gotta do at this point.

Force2b_MikeForce2b_Mike

Hadn't thought about losing the inputField formatting. That's a real shame. Unfortunately, I can't think of a work around for that. I'll reply back again if I can come up with something.

 

Mike 

TomSnyderTomSnyder

Trying to do same,  any way this can be accomplished?  

what is the apex datatype the value attribute it is expecting?  

 

I wrapped in a custom obj and tried to pass as Schema.SobjectField with no luck.

 

Ex. 

 

 

 ff.field = ((Schema.Sobjectfield)myLead.get(myFieldName));

 

... 

 

<apex:inputField value="{!f.field}"/> 

 

jiforcejiforce
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_dynamic_vf.htm