[SalesForce] Access label of Field Set in Visualforce

I am using Field Set in Visualforce pages to render user-defined fields.

<apex:pageBlockSection title="HARDCODED TITLE">
    <apex:repeat value="{!$ObjectType.Account.fieldsets.SystemFields}"
       ....
    </apex:repeat>
</apex:pageBlockSection>

Instead of giving the enclosing section a hard-coded title I would like to dynamically reference / display the Field Sets Label like this:

<apex:pageBlockSection title="{!$ObjectType.Account.fieldsets.SystemFields.Label}">
    <apex:repeat value="{!$ObjectType.Account.fieldsets.SystemFields}"
       ....
    </apex:repeat>
</apex:pageBlockSection>

This is not possible as the variable exposes a FieldSetMember Array. Is there any workaround (without additional Controller code) or an Idea to vote for?

Best Answer

Late answer but I just worked on my solution for this and wanted to share. There is not solution without controller code that I can find but here's one with minimal additions.

Getting a single fieldset name

To get the name of a single fieldset you can create a variable in the controller:

public String fieldSetName { get; set; }

then in the constructor you could use:

fieldSetName = Schema.SObjectType.Case.fieldSets.getMap().get('fieldSetApiName').getLabel();

and finally in the VF page:

<apex:pageBlockSection title="{!fieldSetName}">

Multiple fieldsets

If you are working with multiple field sets my solution has been to create a map of titles.

public Map<String,String> fieldSetNames { get; set; }

Then use a function you can call from the constructor.

public void loadFieldsSetMap(){
    Map<String, Schema.FieldSet> fsMap = Schema.SObjectType.Account.fieldSets.getMap();
    fieldSetNames = new Map<String, String>();
    for(String i : fsMap.keySet() ){
        fieldSetNames.put(i, fsMap.get(i).getLabel());      
    }
}

In the page I then call the relevant map entry with the name in lowercase

<apex:pageBlockSection title="{!fieldSetNames['fieldsetapiname']}">
<apex:repeat value="{!$ObjectType.Account.fieldsets.fieldSetApiName}"
   ....
</apex:repeat>

Hope this helps.

Related Topic