Working with Fieldsets

Fieldsets are one of the feature that you may use to make your visualforce dynamic. They are dynamic and allows display of component on the VF page dynamically. We  can add, remove, or reorder fields in a field set to modify the fields presented on the Visualforce page without modifying any code. Fieldsets are not very new to Force.com, but I have a habit searching for syntax before writing one, so writing this one for my reference and all others like me ;) .

Below are some of the example to show how fieldsets can be used in VF page.


  • Simplest example
<apex:page controller="Account_Con">   
   <apex:repeat value="{!$ObjectType.Account.FieldSets.AccountDetails}" var="f">    
    <apex:outputText value="{!myAccount[f]}" /><br/>   
   </apex:repeat>   
  </apex:page>   


  • To render a PageBlocksection
 <apex:page controller="Account_Con">  
   <apex:pageBlockSection>  
        <apex:repeat value="{!$ObjectType.Account.FieldSets.AccountDetails}" var="f">    
            <apex:outputField value="{!myAccount[f]}" />   
        </apex:repeat>   
   </apex:pageBlockSection>  
  </apex:page>   

  • To render a PageBlockTable
 <apex:page controller="Account_Con">  
   <apex:pageBlockSection>  
        <apex:pageBlockTable>  
             <apex:repeat value="{!$ObjectType.Account.FieldSets.AccountDetails}" var="f">    
                 <apex:column value="{!myAccount.Contacts[f]}" />  
             </apex:repeat>  
     </apex:pageBlockTable>         
   </apex:pageBlockSection>  
  </apex:page>   

All the above listed example dynamically display the records from the fieldset but what about the controller?
we can't just hardcode the queries here.To further extend the solution grab the code for the AVCommonUtil Class that generates query from fieldset/s.

Solution
You can use the util class to generate query dynamically from the fieldsets.

public class Account_Con(){   
  public Account myAccount{get;set;}   
  public AccountCon(){   
      AVCommonUtils util = new AVCommonUtils();  
      String query = util.generateQueryFromFieldSet('Account','AccountDetails',null,'LIMIT 1');  
       accObj = database.query(query);    
  }   
}   

1 comment:

  1. a Thảo Mộc SV là thảo dược được nghiên cứu bởi Viện Khoa học Ứng Dụng Y Dược Phương Đông kết hợp cùng Công ty Cổ Phần Thảo Dược Thiên Nhiên Việt Nam phối hợp sản xuất. Thảo mộc SV gồm các loại dược liệu quý hiếm được sản xuất trên dây chuyền hiện đại tại nhà máy đạt chuẩn GMP. Thảo Mộc SV là giải pháp hữu hiệu giúp giảm nguy cơ mắc các bệnh viêm nhiễm, viêm đường hô hấp do vi khuẩn, virus gây ra.

    ReplyDelete