Field Set in Salesforce explained with an Example

field set in salesforce,sfdcpanda, sfdc panda, salesforce field set tutorial, salesforce fieldset example

Field set in salesforce is grouping the fields of same object type and it is very useful when we’re working with managed package and reordering the fields in field set without touching the code.

Why Field set is very¬†useful when we’re working with managed package ?

In managed package you can’t edit either visualforce page or apex class, There comes a requirement change where you want to reorder the fields in visualforce page. For that you can go ahead with Field set concept and you can do reordering the fields in created Field set under standard or custom object as shown below.

In quick find search bar on the top left corner, type Field set and click new(Go to Setup > Customize > Accounts > Field Set), you will see a screen like below and now you just want to drag and drop the fields and also you can do reordering here as shown in red arrows. Once you done save the field set you created ‘FS’.

drag and drop of fields in salesforce field set, field set salesforce, sfdcpanda, sfdc panda


Apex Class (Field Set in Salesforce):-

In the below Apex class you can see that through this ‘SObjectType.Account.FieldSets.FS.getFields()’ salesforce api method i am accessing each field values which is defined under ‘FS‘ as shown in the above screenshot.

public class FieldSetController {
 public String querystr{get;set;}
 public List<Account> al{get;set;}
 public FieldSetController (){
 querystr = 'select id';
 for(Schema.FieldSetMember fsm :SObjectType.Account.FieldSets.FS.getFields()) {
 querystr += ', ' + fsm.getFieldPath();
 querystr+= ' from Account limit 2';
al= Database.query(querystr);

Finally concatenating all field values from Field set ‘FS‘ and performing a query using ‘Database.query(querystr);’ method to retrieve records from DB.

Visualforce page (Field Set in Salesforce) :-

In Visualforce page, I am iterating over each value through <apex:repeat> tag to display the queried values from DB.

<apex:page controller="FieldSetController">
 <apex:form >
 <apex:pageblock > 
 <apex:pageBlockTable value="{!al}" var="acc">
 <apex:repeat value="{!$ObjectType.Account.fieldsets.FS}" var="FSvalues">
 <apex:column value="{!acc[FSvalues]}">


Be the first to comment

Leave a Reply

Your email address will not be published.