You can also use dynamic bindings to display field sets on your Visualforce pages. A field set
is a grouping of fields. For example, you could have a field set that
contains fields describing a user's first name, middle name, last
name, and business title.If the page is added to a managed package,
administrators can add, remove, or reorder fields in a field set to
modify the fields presented on the Visualforce page without
modifying any code. Field sets are available for Visualforce pages on API version
21.0 or above.
Field sets are referenced in Visualforce by combining the $ObjectType global variable with the keyword FieldSets. For example, if your Contact object has a field set called properNames that displays three fields, your Visualforce page can reference the field data through the following iteration:
You can also choose to render additional information, such as field
labels and data types, through the following special properties on
the fields in the field set:
Property Name
Description
DBRequired
Indicates whether the field is required for the object
FieldPath
Lists the field’s spanning info
Label
The UI label for the field
Required
Indicates whether the field is required in the field set
Type
The data type for the field
For example, you can access the labels and data types for
the fields in properNames like this:
You can have up to 50 field sets referenced
on a single page.
If a Visualforce page is added to a managed package and distributed, subscribers
can edit the properNames field set. The logic
for generating the Visualforce page remains the same, while the presentation differs based on each
subscriber’s implementation. To reference a field set from
a managed package, you must prepend the field set with the organization’s
namespace. Using the markup above, if properNames comes from an organization called Spectre, the field set is referenced
like this:
Fields added to a field set can be in one of
two categories:
If a field is marked as Available for the Field Set, it exists in the field set, but the developer hasn’t presented
it on the packaged Visualforce page. Administrators can display the field after the field
set is deployed by moving it from the Available column to the In the Field Set column.
If a field is marked as In the Field Set,
the developer has rendered the field on the packaged Visualforce page by default.
Administrators can remove the field from the page after the field
set is deployed by removing it from the In the Field Set column.
The order in which a developer lists displayed fields determines
their order of appearance on a Visualforce page.
As a package developer, keep the following
best practices in mind:
Subscribers with installed field sets can add fields that your
page didn’t account for. There is no way to conditionally omit
some fields from a field set iteration, so make sure that any field
rendered through your field set works for all field types.
We recommend that you add only non-essential fields to your field
set. This ensures that even if a subscriber removes all fields in
the field set, Visualforce pages that use that field set still function.