Dynamic Field References

Dynamic field references are designed for situations in which you need to be flexible about which field to render. Ordinary field references are found in Visualforce components and determine their relationship to the controller. For example, an outputText component with content {!project.Name} renders the Name field of the object named project in the controller. The equivalent dynamic field reference is {!project[field]}, where field is a String value containing the name of the field to display.

The data referenced by a dynamic field reference must be available at runtime or an error will occur. If you’re using a standard controller, call the method addFields to notify the controller about new fields if possible, and it will take care of retrieving the data. For custom controllers, controller extensions, or queries involving related objects, build a dynamic SOQL query string and execute it with Database.query.

Listings 7.15 and 7.16 provide the Visualforce controller and page code for a simple example of dynamic field references. The Visualforce page renders a simple XML-encoded collection of Project records, embedded in HTML. The determination of which fields to display from each Project record is determined dynamically inside the controller. The fields are rendered in the page using two nested repeat components. The outer repeat iterates over an array of Project records returned by the controller. The inner repeat cycles through each field name from the controller, combining it with the record reference to obtain the value of that field for the current record.

Listing 7.15 Visualforce Controller Using Dynamic Field References


public class MyPageController7_16 {
  public List<String> fields { get; set; }
  public List<Project__c> records { get; set; }
  public MyPageController7_16() {
    fields = new String[] { 'Id', 'Name', 'CreatedDate' };
    records = [ SELECT Name, CreatedDate FROM Project__c ];
  }
}


Listing 7.16 Visualforce Page Using Dynamic Field References


<apex:page controller="MyPageController7_16">
<pre>
&lt;projects&gt;
  <apex:repeat value="{!records}" var="record">
  &lt;project&gt;
    <apex:repeat value="{!fields}" var="field">
    &lt;{!field}&gt;{!record[field]}&lt;/{!field}&gt;
    </apex:repeat>
  &lt;/project&gt;
  </apex:repeat>
&lt;/projects&gt;
</pre>
</apex:page>


Using Field Sets

Imagine the fields variable in Listing 7.15, which contains the list of field names to display on the Visualforce page, must be maintained by a nondeveloper. You could create a custom object to store the fields in the database and build a Visualforce user interface to manage them. Or you could use field sets and avoid all of that work.

A field set is a user-defined ordered list of fields on an object that can be referenced from Visualforce or Apex. A field set is editable using an administrative user interface built in to Force.com, leaving the code that uses it unchanged. For custom objects, go to the App Setup area; click Create, Objects; select the object; and find its Field Sets section. Standard objects are also in the App Setup area under Customize.

Once a field set is created, it can be referenced in a Visualforce component with the syntax {!$ObjectType.ObjectName.FieldSets.FieldSetName}, where ObjectName is the name of the standard or custom object that the field set is defined on, and FieldSetName is the name of the field set.

The fields of a field set are automatically loaded by the standard controller. For custom controllers, add accessors for the fields and dynamically construct SOQL from the field set to ensure the data is available to the page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.121.54