Friday, 6 February 2015

Field Sets



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. Field sets can be referenced on Visualforce pages dynamically. 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.

Step 1: Create Field set in Account Object.

Setup->Customize->Account->Field Sets
Drag and Drop the fields required for the Field Set

Step 2: Create Visual Force Page


 <apex:repeat value="{!lstAccount}" var="acc"> 
 <table width="100%">
   <apex:repeat value="{!$ObjectType.Account.FieldSets.Account_FieldSet}" var="fieldSet"> 
     <tr> 
      <td>
        {!fieldSet.Label}
      </td>   
      <td>
       <apex:inputField value="{!acc[fieldSet]}"/>
      </td>
      </tr>   
       </apex:repeat>
      </table>
</apex:repeat>

Step 3: Write the Controller for the VisualForce Page

public with sharing class DynamicTabController 
{     public List<Account> lstAccount{get;set;}          public DynamicTabController()     {         lstAccount=new List<Account>();                 String query = 'SELECT ';                 for(Schema.FieldSetMember f : this.getFields())                  {                     query += f.getFieldPath() + ', ';                 }                 query += '  Id FROM Account limit 5 ';                 System.debug('query ------>'+query );                 lstAccount = Database.query(query);             }     public List<Schema.FieldSetMember> getFields()      {         return SObjectType.Account.FieldSets.Account_FieldSet.getFields();     } }

Thanks,
Amit Chaudhary

1 comment: