FieldSet Class
Namespace
Usage
1Map<String, Schema.FieldSet> FsMap =
2 Schema.SObjectType.Account.fieldSets.getMap();Field sets are also available from sObject describe results. The following lines of code are equivalent to the prior sample:
1Schema.DescribeSObjectResult d =
2 Account.sObjectType.getDescribe();
3Map<String, Schema.FieldSet> FsMap =
4 d.fieldSets.getMap();1Schema.FieldSet fs1 = Schema.SObjectType.Account.fieldSets.getMap().get('field_set_name');
2Schema.FieldSet fs2 = Schema.SObjectType.Account.fieldSets.field_set_name;Example: Displaying a Field Set on a Visualforce Page
1public class MerchandiseDetails {
2
3 public Merchandise__c merch { get; set; }
4
5 public MerchandiseDetails() {
6 this.merch = getMerchandise();
7 }
8
9 public List<Schema.FieldSetMember> getFields() {
10 return SObjectType.Merchandise__c.FieldSets.Dimensions.getFields();
11 }
12
13 private Merchandise__c getMerchandise() {
14 String query = 'SELECT ';
15 for(Schema.FieldSetMember f : this.getFields()) {
16 query += f.getFieldPath() + ', ';
17 }
18 query += 'Id, Name FROM Merchandise__c LIMIT 1';
19 return Database.query(query);
20 }
21}1<apex:page controller="MerchandiseDetails">
2 <apex:form >
3
4 <apex:pageBlock title="Product Details">
5 <apex:pageBlockSection title="Product">
6 <apex:inputField value="{!merch.Name}"/>
7 </apex:pageBlockSection>
8
9 <apex:pageBlockSection title="Dimensions">
10 <apex:repeat value="{!fields}" var="f">
11 <apex:inputField value="{!merch[f.fieldPath]}"
12 required="{!OR(f.required, f.dbrequired)}"/>
13 </apex:repeat>
14 </apex:pageBlockSection>
15
16 </apex:pageBlock>
17
18 </apex:form>
19</apex:page>FieldSet Methods
The following are methods for FieldSet. All are instance methods.
getDescription()
Signature
public String getDescription()
Return Value
Type: String
Usage
Description is a required field for a field set, intended to describe the context and content of the field set. It’s often intended for administrators who might be configuring a field set defined in a managed package, rather than for end users.
getFields()
Signature
public List<FieldSetMember> getFields()
Return Value
Type: List<Schema.FieldSetMember>
getNamespace()
Signature
public String getNamespace()
Return Value
Type: String
Usage
The returned namespace is an empty string if your organization hasn’t set a namespace, and the field set is defined in your organization. Otherwise, it’s the namespace of your organization, or the namespace of the managed package containing the field set.