Thursday, July 10, 2008

Visualforce Helps Developers Build Interesting Applications Faster

I've written before about how programming in Apex allows developers to be much more productive. Visualforce similarly helps developers avoid writing a lot of boring code.

If Visualforce and Apex were a typical development platform, the canonical way of populating a data bound picklist would be something like this:

Visualforce markup:

<apex:pageBlockSectionItem>
<apex:outputLabel value="Category" for="expenseCategory"/>
<apex:selectList multiselect="false" id="expenseCategory" required="true" size="1" value="{!expense.Category__c}">
<apex:selectOptions value="{!ExpenseCategories}"/>
</apex:selectList>
</apex:pageBlockSectionItem>


With the following controller code:
public List getExpenseCategories()
{
Map allObjectTypesMap = Schema.getGlobalDescribe();
Schema.DescribeFieldResult f = Schema.sObjectType.Expense__c.fields.Category__c;
List options = new List();
options.add(new SelectOption('--None--', '--None--'));
for(Schema.PicklistEntry ple : f.getPicklistValues())
{
options.add(new SelectOption(ple.getValue(), ple.getLabel()));
}
}



One of the benefits of developing on the Force.com platform is that it is very data-driven and has native access to your application's metadata. As a result, the code below is equivalent to the code written above and is much less verbose:

<apex:inputField id="expenseCategory" required="true" value="{!expense.Category__c}"/>

As a result, developers can spend their time building really interesting applications and let the Force.com platform worry about the plumbing.

No comments: