I like to add a field to a VF page when a field is added to a object by admin with out re coding the VF page. Is their any way to add fields to VF page automatically when a field get created on the object.
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
You can create Fieldset on that object and then iterate that field set on your VF page.
You can iterate field set like this on VF page -
and in controller you can do like this -
Yes! Fieldsets were created to meet exactly this requirement. From the docs:
Two solutions. The first, proposed by grigriforce already, is to use Fieldsets. They have the advantage that an admin gets to specify which fields they want, and where, in the locations that use that fieldset. They carry the disadvantage that the admin must do this, so in the case where you always want new fields to show up automatically, it's more work for no gain.
The second alternative is to use the somewhat under-the-radar feature of dynamic field bindings in VF. You use the Apex Describe API to get a list of fields (in this case, all editable fields) for an object, then you dynamically bind them in your page. Example controller properties:
Example VF page: