Thursday, February 25, 2016

Using Field Set on Visual Force Page and Controller

We can dynamically bind fields on our visual force page using Field Set. It is very useful when we are working with managed package. We can add, remove, or reorder fields in a field set to modify the fields presented on the Visualforce page without modifying any code. Field sets are available for Visualforce pages on APIversion 21.0 or above. You can have up to 50 field sets referenced on a single page.

step 1

Create Custom Object Name  Fieldset Demo , API name is Fieldset_Demo__c

step 2
create Custom Field
(a) First Name (text 80)
(b) Last Name(text 80)
(c) Email(Email)
(d) phone(phone)
API Name and size also shown in below image.

Step 3
   Mouse over on the Field Sets of Current Custom object. its show dialog box with new button. click on the new . see below image
Yellow Colour highlighted

Step 4

Click on the new button it will display a window which is shown in below image. fill all the field and save it












Step 5
 Drag and drop field on "In the Field Set"  like Firstname lastname email and phone i want to show on the page.
see below image
After that click on the save button

Step 6

Click and open your Custom object (
Fieldset Demo) and save 3-4 records

Step 7
create class (controller)

public class FieldsetController {    
    public List <Fieldset_Demo__c> fList{get;set;}
    public String fields = '';
    
        public FieldsetController() {
        fList = new List<Fieldset_Demo__c>();
        for(Schema.FieldSetMember f : SObjectType.Fieldset_Demo__c.FieldSets.My_Fieldset.getFields()) {
            fields += f.getFieldPath() + ',';
        }
        fields = fields.removeEnd(',');
        String queryString = 'SELECT '+fields+' FROM Fieldset_Demo__c';
        fList = Database.query(queryString);
        
        }
}

NOTE: 
Fieldset_Demo__c this is API name of object      My_Fieldset this is fieldset name    
Step 8
Create page
<apex:page controller="FieldsetController ">
<apex:pageBlock>
  <apex:pageBlockTable value="{!fList}" var="templist" rules="rows"> 
                 <apex:repeat value="{!$ObjectType.Fieldset_Demo__c.FieldSets.My_Fieldset}" var="f"> 
                     <apex:column headervalue="{!f.label}">
                        <apex:outputField value="{!templist[f.fieldPath]}"> </apex:outputField>
                     </apex:column>  
                    </apex:repeat>
              
           </apex:pageBlockTable>
  </apex:pageBlock>
</apex:page>

Result 





Thanks
Vipin Indora







No comments:

Post a Comment