Feb 22

Fieldset visualforce page salesforce

Fieldset visualforce page salesforce

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.

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

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

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

  • mahesh

    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………………………….

    • 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

    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

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