Fieldset apex code salesforce

Fieldset apex code salesforce

Fieldset apex code 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.

If we are using standard controller, then using fieldset in visualforce is straight forward. You can view my previous post for using fieldset in visualforce pages.

But if we are using custom controller or extension, then we may need to query fieldset fields in SOQL query. So we will use dynamic SOQL query for querying all fields of fieldset.

By using dynamic SOQL query in Apex and using fielset in visualforce and Apex code. Our code will become dynamic, then we can add, remover or reorder fields in fieldset. By doing so, our code will become dynamic. There is no need to modify code when we will do any change in fieldset.

In the example below, we will use fieldset to display account list on visualforce page. We will use dynamic SOQL query in Apex code.

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

Visualforce Page:

<apex:page controller="AccountFieldSetController" tabStyle="Account">
  <apex:form >
      <apex:pageblock >        
          <apex:pageBlockSection title="Account list" collapsible="false">
             <apex:pageBlockTable value="{!accList}" var="acc">
                 <apex:repeat value="{!$ObjectType.Account.fieldsets.accountFieldSet}" var="fieldValue">
                     <apex:column value="{!acc[fieldValue]}">
                     </apex:column>
                 </apex:repeat>
             </apex:pageBlockTable>
          </apex:pageBlockSection>
          
          <apex:pageBlockSection title="Account Dynamic query" collapsible="false">
              <apex:outputText value="Query is: {!queryString}" />
          </apex:pageBlockSection>
          
      </apex:pageblock>
    </apex:form>
</apex:page>

Apex Code:

public class AccountFieldSetController {
    public String queryString{get;set;}
    public List<Account> accList{get;set;}
    public AccountFieldSetController(){
        queryString = 'select id';
        for(Schema.FieldSetMember fld :SObjectType.Account.FieldSets.accountFieldSet.getFields()) {
         queryString += ', ' + fld.getFieldPath();
        }
        queryString += ' from Account limit 5';
        
        acclist = Database.query(queryString);
    }
    
}

We will get following output in visualforce page:

FieldSet Apex Code Salesforce Example

FieldSet Apex Code Salesforce

For more details about using fieldset in visualforce, refer to Fieldset visualforce page salesforce.

For more details about fieldset refer this official link.

 

Permanent link to this article: https://www.sfdcpoint.com/salesforce/fieldset-apex-code-salesforce/

10 comments

Skip to comment form

    • mahesh on February 27, 2014 at 12:32 pm
    • Reply

    iam getting Error: Unknown property ‘Account.fieldsets.accountFieldSet’ , so pls update this

    1. Hello Mahesh. Thanks for your comment. I checked, it is working fine. I believe that you have not created fieldset. So create a fieldset with name ‘accountFieldSet’ on account as mentioned in post. Then try to run code.

        • k mahesh on May 12, 2014 at 11:24 am
        • Reply

        Hi Ankush Dureja

        I have one requirement, Actually I created objects and these objects related fields using metadata api in an visualforce page.But my requirement is how to retrieve created fields(Using visualforce page ) in object Dynamically using visualforce page and apex class.Please help me

    • kiran raju on August 19, 2014 at 7:34 pm
    • Reply

    Iam getting error like discan u help me here

    Error: Compile Error: accountfieldset is not a fieldSet of Account

    1. Hello Kiran,

      You need to create a fieldset with name ‘accountfieldset’ for this example to work properly. To create a new fieldset, Go to Setup > Customize > Accounts > Field Set.

      I have explained complete process in above example.

      Thanks
      Ankush Dureja

    • suman myana on April 11, 2017 at 7:26 am
    • Reply

    how to create a filter configuration with field sets

    • Chaitanya K on May 30, 2017 at 12:55 pm
    • Reply

    Hi Can you please help me that how to cover the field set in test class.

    • Sriram on August 17, 2017 at 6:37 am
    • Reply

    Hi Ankush,

    I could see only “In the Field Set” panel and No “Available Fields” (Left side ) panel when I am trying to Create/Edit Field set for Account object.
    Is there any setting which I have to enable to have “Available Fields” panel.

    Thanks

    • venkat Goli on March 22, 2018 at 1:17 pm
    • Reply

    is required and must be the outermost tag in the markup at line 1 column 1
    Im getting this error please hem me out.

    • Supriya on September 9, 2021 at 10:49 pm
    • Reply

    Hello Ankush,

    Can we create a new filed set and assigned to Page UI , can it be assign without any change in visualforce page?

Leave a Reply

Your email address will not be published.