Field sets
You can use dynamic bindings to display field
sets on your Visualforce pages.
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. If the page is added to a managed package, administrators can add,
remove, or reorder fields in a field set to modify the fields presented on the Visualforce page without modifying
any code.
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.
Click on new. Enter all mandatory fields. Also drag and drop all required fields in fieldset.
Working with Field Sets
Using Visualforce:
Field sets can be directly referenced in Visualforce by combining the $ObjectType global variable with the
keyword FieldSets.
For example,
if your Lead
object has a field set called lead_fields that displays six fields, your Visualforce page can reference the field data
through the following iteration.
<apex:page standardController="Lead">
<apex:form >
<apex:pageblock >
<apex:pageBlockSection title="lead detail">
<apex:repeat
value="{!$ObjectType.Lead.fieldsets.lead_fields}"
var="f">
<apex:Inputfield value="{!lead[fieldValue]}"/>
</apex:repeat>
</apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>
You can also choose to render additional information, such as
field labels and data types, through the following special properties on the
fields in the field set.
Property Name
|
Description
|
DBRequired
|
Indicates whether the field is required for the object
|
FieldPath
|
Lists the field’s spanning info
|
Label
|
The UI label for the field
|
Required
|
Indicates whether the field is required in the field set
|
Type
|
The data type for the field
|
<apex:page
standardController="lead">
<apex:pageBlock title="Fields in
Proper Names">
<apex:pageBlockTable
value="{!$ObjectType.lead.FieldSets.lead_fields}"
var="f">
<apex:column
value="{!f}">
<apex:facet
name="header">Name</apex:facet>
</apex:column>
<apex:column
value="{!f.Label}">
<apex:facet
name="header">Label</apex:facet>
</apex:column>
<apex:column value="{!f.Type}"
>
<apex:facet
name="header">Data Type</apex:facet>
</apex:column>
<apex:column
value="{!f.Required}" >
<apex:facet
name="header">Required</apex:facet>
</apex:column>
<apex:column
value="{!f.FieldPath}" >
<apex:facet
name="header">FieldPath</apex:facet>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
Working with Field Sets Using Apex
Fields in a field set are automatically loaded when your Visualforce page uses a standard controller.
When using a custom controller, you need to add the required
fields to the SOQL query for the page.
Apex provides two Schema objects
that allow you to discover field sets and the fields they contain, Schema.FieldSet and Schema.FieldSetMember.
<apex:page
controller="leadfieldset" sidebar="false">
<apex:form >
<apex:pageBlock title="Lead
Details">
<apex:pageBlockSection
title="Lead Name">
<apex:inputField
value="{!lead.Name}"/>
</apex:pageBlockSection>
<apex:pageBlockSection
title="Dimensions">
<apex:repeat
value="{!fields}" var="f">
<apex:inputField
value="{!lead[f.fieldPath]}"
required="{!OR(f.required,
f.dbrequired)}"/>
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
public class leadfieldset{
public lead lead{ get; set; }
public leadfieldset() {
this.lead= getlead();
}
public List<Schema.FieldSetMember>
getFields() {
return
SObjectType.lead.FieldSets.lead_fields.getFields();
}
private lead getlead() {
String query = 'SELECT ';
for(Schema.FieldSetMember f : this.getFields()) {
query += f.getFieldPath() + ', ';
}
query += 'Id, Name FROM lead LIMIT 1';
return Database.query(query);
}
}
Field Set Considerations
Fields added to a field
set can be in one of two categories:
·
If a field is marked
as Available for the Field Set, it exists in the field set, but the developer hasn’t presented
it on the packaged Visualforce page. Administrators can display the
field after the field set is deployed by moving it from the Available column to
the In the Field Set column.
·
If a field is marked
as In the Field Set,
the developer has rendered the field on the packaged Visualforce page
by default. Administrators can remove the field from the page after the field
set is deployed by removing it from the In the
Field Set column.
The order in which a developer lists displayed fields determines
their order of appearance on a Visualforce page.
No comments:
Post a Comment