Dynamic Field Sets

One of the things I love about Force.com is how easy it can be to stitch together blocks of functionality provided in the platform, saving you development effort. Spending a little extra time working out how to best use platform features can also save effort downstream – when you align yourself with the platform you can usually benefit from enhancements and additional functionality very quickly as it is added by Salesforce.

I was recently asked whether we could assign Field Sets for Visualforce pages to a user’s profile (without using Field Level Security to limit access to the fields entirely). Although this is not out-of-the-box functionality, I felt sure that this should be possible with a little effort by stitching together Field Sets and Hierarchical Custom Settings.

Hierarchical Custom Settings allow admins to apply configuration at org, profile or user level. This type of custom setting can be used to great effect in formula fields – you can modify the behaviour of the formula depending on a custom setting value, which an admin can configure differently for each profile or user.

Field Sets allow admins to influence how Visualforce pages work by adding and removing fields within portions of a page without having to change the Visualforce code. In this trivial example an admin could add or remove fields from a Field Set imaginatively called “FS1” and the page will display the values for each of the fields included in the Field Set:

<apex:page standardController="Account">
  <apex:repeat value="{!$ObjectType.Account.FieldSets.FS1}" var="f">
    <apex:outputText value="{!Account[f]}" /><br/>

This is great, although we have hard-coded the name of the Field Set into the page. What we want to do is to pick up the name of the field set to be used from a Hierarchical Custom Setting so that different lists of fields can be set up by an admin for use with different users and profiles.

We can access Hierarchical Custom Setting values in Visualforce using the $Setup global variable. So, if we have a Hierarchical Custom Setting called “Account Field Set Settings” and this setting has a text field called “Field Set Name”, then we might access the value of this setting in Visualforce like this:


All that should now be needed is to use dynamic binding to stitch the Field Set name into the Visualforce page:

<apex:page standardController="Account">
  <apex:repeat value="{!$ObjectType.Account.FieldSets[$Setup.AccountFieldSetSettings__c.FieldSetName__c]}" var="f">
    <apex:outputText value="{!Account[f]}" /><br/>

Now, assuming we have a Field Set, called “FS1” and we set up an org-level Custom Setting record in which we provide the Field Set name, we would expect the page to display with any fields we have added to the FieldSet.

Unfortunately what we actually get is:

SObject row was retrieved via SOQL without querying the requested field: ...

It would appear that the dynamic binding of a Field Set does not provide the platform with enough of a hint to query the necessary fields.

No matter, we can remedy this with a little Apex in a controller extension:

public class AccountFieldSetController{
  public AccountFieldSetController(ApexPages.StandardController sc){
    String fsn = AccountFieldSetSettings__c.getInstance().FieldSetName__c;
    List<String> fieldNames = new List<String>();
      for(Schema.FieldSetMember f : SObjectType.Account.FieldSets.getMap().get(fsn).getFields()){

This controller extension gets the Field Set name from the Custom Setting, then uses the name to get the Field Set members, from which a list of field names is built and added to the standard controller.

Finally we need to add this controller extension to our Visualforce page:

<apex:page standardController="Account" extensions="AccountFieldSetController">

Now an admin can decide which Field Set is assigned to which users, individually, or by profile, and the page will display the fields according to the appropriate Field Set.

This entry was posted in Tutorials and tagged , , , . Bookmark the permalink.

1 Response to Dynamic Field Sets


    Thank you. Really helpful!! 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s