Tuesday 15 September 2015

Using Field Sets in Visual force pages

Field Sets in Visualforce?

Field Set in salesforce is a grouping of fields that can be used by developers to build custom visual force pages to display the fields and data using visualforce binding expressions. Administrators can later add or remove fields from the field set and the visualforce page will reflect to these changes.

Benefits?

1. By making visualforce page to use field set, we can avoid recurring changes/enhancements to the same component again and again when a field has to be added or removed
2. Free from migrations, test class coverage as no changes at component level is required
3. Component becomes dynamic
4. By passing the field set name dynamically through code, we can make the same visual force page to use different field set displaying different set of fields based on any criteria/logic
5. As field set allows selection of relationship fields and visualpage displays the same
6. Helps managed package users/subscribing orgs to use managed package field sets and let them to customize managed package VF pages to customize (only if their vendor built pages using field sets)

Below markup explains the use of Field Sets in visual force page:

Visualforce page


<!-- 
Code Name : VisualforceUsingFieldSet
Code Type  : Visualforce
Description : Using Field Set in Visualforce page
Author        : Bharathimohan Ramamurthy
/-->
<apex:page standardController="Product2" recordSetvar="products">
<apex:form >
<apex:pageBlock title="Using Field Sets in Visualforce Page">
    <apex:pageBlockButtons location="top">
        <div style="float:left;font-weight:bold;">
            <apex:outputLabel value="Select View:"/>
            <apex:selectList value="{!filterId}" size="1">
                <apex:actionSupport event="onchange" rerender="myTable"/>
               <apex:selectOptions value="{!listviewoptions}"/>
            </apex:selectList>
        </div>
        <div style="float:center;font-weight:bold;">
            <apex:commandLink action="{!first}">First</apex:commandlink> &nbsp;&nbsp;
            <apex:commandLink action="{!previous}">Previous</apex:commandlink> &nbsp;&nbsp;
            <apex:commandLink action="{!next}">Next</apex:commandlink> &nbsp;&nbsp;
            <apex:commandLink action="{!last}">Last</apex:commandlink> &nbsp;&nbsp;
        </div>
    </apex:pageBlockButtons>
      <apex:pageBlockTable value="{!products}" var="v" id="myTable">
          <apex:repeat value="{!$ObjectType.Product2.FieldSets.fieldSetForPagination}" var="f">
              <apex:column value="{!v[f.FieldPath]}"/>
        </apex:repeat>
      </apex:pageBlockTable>           
</apex:pageBlock>
</apex:form>
</apex:page>


Preview


Using Field Sets in Visual force pages


{!$ObjectType.Product2.FieldSets.fieldSetForPagination}

$ObjectType - Global Variable
Product2 - Object API Name
FieldSets - Keyword (use as such)
fieldSetForPagination - Your field set api name goes here. To access managed package field sets, use namespace as prefix with ( __ ) in between.

Considerations

1. Field will be required if visual force page is in edit mode and if the field set field is marked as required (DB required attribute determines this)
2. Maximum number of field sets that can be referenced in a page is 50 (..way beyond than ideal need for anyone)

No comments:

Post a Comment