Field Set(s) Creation and displaying a field set on a vf page #inSalesforce

Creation of Field Sets :

This is a grouping of fields.  Example: Group of user's first name, middle name, last name, and business title. 

Steps for creating :

Object Manager | the Object | Field Sets | click New | Enter a Field Set Label | Fill up 'Where is this used?' area : Write about which Visualforce pages use the field set, and for what purpose. | Save your changes.

To add fields to the field set, drag the fields from the object palette and drop them into the Available for the Field Set or the In the Field Set container and make other necessary changes and save.


Displaying a Field Set on a Visualforce Page


//Class


public class AccountDetails {
    public Account acct { get; set; }
    
    public AccountDetails() {
        this.acct = getAccount();
    }
    public List<Schema.FieldSetMember> getFields() {
        return SObjectType.Account.FieldSets.Info.getFields();
    }
    private Account getAccount() {
        String query = 'SELECT ';
        for(Schema.FieldSetMember f : this.getFields()) {
            query += f.getFieldPath() + ', ';
        }
        query += 'Id, Name FROM Account LIMIT 1';
        return Database.query(query);
    }
}



//Page


<apex:page controller="AccountDetails">
    <apex:form >
      <apex:pageBlock title="Info">
          <apex:pageBlockSection title="Quick Info">
              <apex:inputField value="{!acct.Id}"/>
          </apex:pageBlockSection>
      
          <apex:pageBlockSection title="More Info">
              <apex:repeat value="{!fields}" var="f">
                  <apex:inputField value="{!acct[f.fieldPath]}" 
                      required="{!OR(f.required, f.dbrequired)}"/>
              </apex:repeat>
          </apex:pageBlockSection>
  
        </apex:pageBlock>
    </apex:form>  
</apex:page>



Reference: 

https://help.salesforce.com/s/articleView?id=sf.fields_editing_field_sets.htm&type=5

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_fieldsets_describe.htm

 

Comments