You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Field Set in Salesforce


By: Radha

Introduction: 

Field Set is used to group the fields of an object. We can use this in the Visualforce page and Apex class. Dynamically, we can add, remove, and reorder the fields without modifying the code in the VF Page using Field Set.

Creating Field Set: 

We can create field set for all Standard and Custom Objects in our Salesforce Org.

Standard Objects: 

Setup [Symbol] Field Sets [Symbol] New

Custom Objects: 

We can create field set in Object definition page [Symbol] Field Sets [Symbol] New.

Then, we can see the following page. Fill in the necessary information to create the field set and click [Symbol] Save.

Then, it will automatically redirect to the field set page layout. We can see the “Available for the Field Set section” in the field set page layout.

Drag the fields from field panel and drop fields into the “In the Field Set” container, then click Save.

Note: Only the fields “In the Field Set” container will display in the VF page. The order of rendered fields based on the list in the “In the Field Set” container.

We can specify the fields in the field set as required by clicking the wrench icon ().

Field Set in VF Page: 

Field Set can be specified in the VF Page for objects $ObjectType, Object API name, with the variable FieldSets followed by the Field Set API Name.

Standard Object: 

Ex: $ObjectType.Account.FieldSets.Account_Field_Set

Custom Object: 

EX: $ObjectType.Employee__c.FieldSets.Employee_Field_Set

VF Page without Field Set: 

<apex:page standardController=”Account”>

<apex:form >

<apex:pageBlock >

<apex:pageBlockButtons >

<apex:commandButton value=”Save” action=”{!Save}” />

<apex:commandButton value=”Save & New” />

<apex:commandButton value=”Cancel” action=”{!cancel}” />

</apex:pageBlockButtons>

<apex:pageBlockSection title=”Account Information”>

<apex:inputField value=”{!Account.Name}”/>

<apex:inputField value=”{!Account.AccountNumber}”/>

<apex:inputField value=”{!Account.Industry}”/>

<apex:inputField value=”{!Account.BillingStreet}”/>

<apex:inputField value=”{!Account.Email__c}”/>

<apex:inputField value=”{!Account.Fax}”/>

<apex:inputField value=”{!Account.AnnualRevenue}”/>

<apex:inputField value=”{!Account.Call_out_Response_Code__c}”/>

</apex:pageBlockSection>

</apex:pageBlock>

</apex:form>

</apex:page>

VF Page with Field Set: 

<apex:page standardController=”Account”>

<apex:form >

<apex:pageBlock >

<apex:pageBlockButtons >

<apex:commandButton value=”Save” action=”{!Save}” />

<apex:commandButton value=”Save & New” />

<apex:commandButton value=”Cancel” action=”{!cancel}” />

</apex:pageBlockButtons>

<apex:pageBlockSection title=”Account Information”>

<apex:repeat value=”{!$ObjectType.Account.FieldSets.Account_Field_Set}” var=”fields”>

<apex:inputField value=”{!Account[fields]}” />

</apex:repeat>

</apex:pageBlockSection>

</apex:pageBlock>

</apex:form>

</apex:page>

Field attribute such as DBRequired, Required, Label, FieldPath, DataType can be specified using the field set in VF Page.

DBRequired [Symbol] This will show the field as required for the object.

Ex: 

<apex:repeat value=”{!$ObjectType.Account.FieldSets.Account_Field_Set}” var=”fields”>

<apex:inputField value=”{!Account[fields]}” required=”{!fields.DBrequired}”/>

</apex:repeat>

Required [Symbol] It will set the field as required depends on the required field information in the field set.

Ex: 

<apex:repeat value=”{!$ObjectType.Account.FieldSets.Account_Field_Set}” var=”fields”>

<apex:inputField value=”{!Account[fields]}” required=”{!fields.Required}”/>

Label [Symbol] It will show the Label of a field in the field set.

FieldPath [Symbol] It will show the referenced object level of a field in the field set.

Type [Symbol] It will show the data type of the fields in the field set.

Field Set in Apex Class: 

We can use the field set in Apex class using Schema class. The Schema class provides the metadata information. “Schema.FieldSet” is used to find the information such as name, namespace, and label of the field set. “Schema.FieldSetMember” is used to get information about the field details such as fields label, name, namespace, datatype, and dynamic SOQL field path.

Ex: 

String query = ‘SELECT ‘;

for(Schema.FieldSetMember field : this.getFields()) {

query += field.getFieldPath() + ‘, ‘;

}

query += ‘Id, Name FROM Account’;

return Database.query(query);

Limitations: 

  • Field Set is available from API version 21.0 or above.
  • We can add up to 50 Field Set in a VF Page.

Reference: 

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_dynamic_vf_field_sets.htm

https://help.salesforce.com/articleView?err=1&id=fields_editing_field_sets.htm&type=0