Working with Field Sets

A field set is a grouping of fields. For example, you could have a field set that contains fields describing a user's first name, middle name, last name, and business title.

Field sets are available for Visualforce pages on API version 21.0 or above. You can have up to 50 field sets referenced on a single page. An sObject can have up to 2,000 field sets.

Each field set can have up to 25 fields through lookup relationships. Fields can only span one level away from the entity.

Setup Instructions

Select the Account object.

In the Account object detail page, go to Fields & Relationships.

Click "Field Sets" and then "New".

Name your field set (e.g., "Account_Details") and add the fields you want to include in the field set. Save the field set.








Visualfore Page Example

<apex:page standardController="Account">

    <apex:form>
        <apex:pageBlock title="Account Information">
            <apex:pageBlockSection>
                <apex:repeat value="{!$ObjectType.Account.FieldSets.Account_Details}" var="field">
                    <apex:inputField value="{!Account[field]}" />
                </apex:repeat>
            </apex:pageBlockSection>
            <apex:pageBlockButtons>
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>

</apex:page>

Field Sets in Apex

public class FieldSetController {
    // Method to retrieve field metadata from a field set
    public static List<String> getFieldSetFields(String objectName, String fieldSetName) {
        List<String> fieldList = new List<String>();
        
        Schema.SObjectType objectType = Schema.getGlobalDescribe().get(objectName);
        if (objectType != null) {
            Schema.DescribeSObjectResult objectDescribe = objectType.getDescribe();
            if (objectDescribe != null && objectDescribe.fieldSets.getMap().containsKey(fieldSetName)) {
                List<Schema.FieldSetMember> fieldSetMembers = objectDescribe.fieldSets.getMap().get(fieldSetName).getFields();
                for (Schema.FieldSetMember fsm : fieldSetMembers) {
                    fieldList.add(fsm.getFieldPath());
                }
            }
        }
        
        return fieldList;
    }
    
    // Method to fetch Accounts using fields from a field set
    public static List<Account> fetchAccountsUsingFieldSet(String fieldSetName) {
        List<String> fields = getFieldSetFields('Account', fieldSetName);
        String soql = 'SELECT ';
        for (String field : fields) {
            soql += field + ', ';
        }
        soql += 'Id FROM Account';
        
        return Database.query(soql);
    }

}

Page

<apex:page controller="FieldSetController">
    <apex:pageBlock title="Dynamic Account Details">
        <apex:pageBlockTable value="{!fetchAccountsUsingFieldSet('Account_Details')}" var="acc">
            <apex:repeat value="{!$ObjectType.Account.FieldSets.Account_Details}" var="field">
                <apex:column headerValue="{!field.label}">
                    <apex:outputField value="{!acc[field.fieldPath]}" />
                </apex:column>
            </apex:repeat>
        </apex:pageBlockTable>
    </apex:pageBlock>

</apex:page>


Salesforce References



Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach