FieldSets in Apex

As of the Summer ’12 release FieldSets can be accessed through Dynamic Apex (or Apex Describe). Remember, FieldSets are admin configured arbitrary groupings of fields, added on the object detail page within setup. With the upcoming release, controller code can now dynamically build a soql query that retrieves the data for the fields in the FieldSet, which in turn can then provide data for Visualforce dynamic bindings. Previously the binding was possible, but there was no way to retrieve the fields to construct the soql query – a serious limitation.

Why is this interesting? Well, consider you’re developing a custom app comprised of Visualforce pages, wouldn’t it be great if new custom fields added later could simply just render in the UI (in a logical position) without modifying the page definition or controller code? Or perhaps that obsolete field you no longer wish to display just disappears..

Admin configurable custom pages (and apps) is a good thing. If you’re building custom pages give some thought to FieldSets and dynamic field rendering, this could save you time and your customer money down the line.

Same principle as Custom Labels, you need to rule the dynamic field rendering requirement out, not in.

[sourcecode language=”java”]
for(Schema.FieldSetMember f : SObjectType.Player__c.FieldSets.Dimensions.getFields()){

%d bloggers like this: