[SalesForce] VisualForce: Add dynamically inputs fields

I have to be able to display, from a custom Object, specific fields.

For example, if the field name contains "_network", I want only to display those fields.

Since at work they will be keeping in creating new fields "_network", I need a system in visualforce that allows me to dynamically display the fields (apex:inputfield) with the constrains described above.

Is this possible by only using visualforce and a controller?

Best Answer

One way is for the controller to provide a list of the field API names (that the controller will also have to query):

public String fields {
    get {
        // Hard coded list here: build your varying list instead
        return new String[] {'Name', 'Birthdate'};
    }
}

public Contact contact {...}

and then use this syntax to reference the relevant SObject fields by the API names:

<apex:repeat value="{!fields}" var ="f">
     <apex:inputField value="{!contact[f]}"/>
</apex:repeat>
Related Topic