Fieldset visualforce page salesforce

Fieldset visualforce page salesforce

Fieldset visualforce page salesforce

How to use fieldset in Salesforce Visualforce pages?

Fieldset:

A field set is a grouping of fields for same object. We can use dynamic bindings to display field sets on our Visualforce pages. Fieldset is very useful when we are working with managed package.

Field sets can be directly referenced in Visualforce by combining the $ObjectType global variable with the keyword FieldSets.

A fieldset is a grouping of fields. For example, fieldset can contain fields like first name, last name, mobile, phone of user object. Consider a scenario, suppose we want to display some fields in a page and we want to keep the flexibility to change which fields are displayed. In such a scenario we should use field sets to add or remove the fields as per requirement.Which ever fields are included in the field set will be displayed in page. For this we will need to create a field set on object we wish, then we can access it directly in visualforce page.

Another scenario is, suppose visualforce code is part of managed package. In that case we can add, remove, or reorder fields in a field set to modify the fields presented on the Visualforce page without modifying any code.

In the example below, we will display one fieldset for account object then we will display fieldset fields on visualforce page.

Click for Demo

Steps to create field set on an object :

First we need to create a fieldset. Go to Setup > Customize > Accounts > Field Set
Click on new. Enter all mandatory fields. Also drag and drop all required fields in fieldset.

Create account field set

Now we will create visualforce code which will use fieldset.
Visualforce Code:

<apex:page standardController="Account">
  <apex:form >
      <apex:pageblock >    
          <apex:pageBlockSection title="Account detail">
             <apex:repeat value="{!$ObjectType.Account.fieldsets.accountFieldSet}" var="fieldValue">
                 <apex:Inputfield value="{!Account[fieldValue]}"/>
             </apex:repeat>
          </apex:pageBlockSection>
      </apex:pageblock>
    </apex:form>
</apex:page>

We can add, remove, or reorder fields in a fieldset to modify the fields presented on the Visualforce page without modifying visualforce page code.

We will get following output in visualforce page:

fieldset Visualforce page

For more details about using fieldset in apex, refer to Fieldset apex code salesforce

Also for more details about fieldset refer this official link.

Permanent link to this article: https://www.sfdcpoint.com/salesforce/fieldset-visualforce-page-salesforce/

5 comments

Skip to comment form

    • mahesh on June 6, 2014 at 4:55 pm
    • Reply

    Hi,
    How to add pagelayout fields to fieldset dynamically(through coding) and how to iterate created fieldset in an visualforce page dynamically.

    please help me………………………….

    1. Hi,
      I have already provided code to iterate created fieldset in an visualforce page dynamically. In above post, I have explained, visualforce page code to retrieve fields dynamically from fieldset. And I think adding pagelayout fields to fieldset dynamically through coding is not possible.

    • Nagaraj SV on April 17, 2015 at 11:01 am
    • Reply

    Hi,
    When I display the page – the fields that are marked as required in my fieldset definition are not displaying as ‘required’ (with ared-line next to them on the page)
    Plsss Help

      • EJ on April 30, 2015 at 10:29 pm
      • Reply

      Hi Nagaraj, did you ever resolve your issue? I’m having the same problem. Thanks!

    • MK on June 3, 2020 at 6:38 am
    • Reply

    If I have a custom object called Servicing_Loan__c and the Fieldset is named Watchlist, would the code be? And will the code work if one of the fields in the Fieldset is a lookup field and will that lookup field be editable?

    I tried to pull in a Fieldset into a lightning page and although all my fields came in, my lookup field wasn’t editable.

Leave a Reply

Your email address will not be published.