Flexible Field Labels

March 23, 2009 · Filed Under Configuration, Native Application, Visualforce, salesforce.com 
While making a Visualforce page to display a Zip/Country -> City, State, County application, it became obvious that labeling fields manually will not satisfy all users, especially international ones. There are a few ways to include a field and its label on a Visualforce page.

The simplest, using OutputField:
VF Page (simple):

<apex:page standardcontroller="Account">
<apex:pageblock>
<apex:pageblocksection>
<apex:outputField value="{!Account.Name}" />
</apex:pageblocksection>
</apex:pageblock>
</apex:page>
code1

The most common (based on code shares and other observations) method allows one to use a label other than the one in Salesforce. It also allows combining of multiple fields while maintaining the standard style:
VF Page (common, but static):

<apex:page standardcontroller="Account">
<apex:pageblock>
<apex:pageblocksection columns="1">
<apex:pageblocksectionitem>
<apex:outputLabel value="Account Name" for="an" />
<apex:outputText value="{!Account.Name}" id="an" />
</apex:pageblocksectionitem>
<apex:pageblocksectionitem>
<apex:outputLabel value="City, State, Zip" for="csz" />
<apex:outputText value="{!Account.BillingCity}, {!Account.BillingState} {!Account.BillingPostalCode}" />
</apex:pageblocksectionitem>
</apex:pageblocksection>
</apex:pageblock>
</apex:page>
code2

And a way to allow field label renaming and translations:
VF Page (more flexible, allowing for renaming and translations):

<apex:page standardcontroller="Account">
<apex:pageblock>
<apex:pageblocksection columns="1">
<apex:pageblocksectionitem>
<apex:outputLabel value="{!$ObjectType.account.fields.name.label}"  for="an" />
<apex:outputText value="{!Account.Name}" id="an" />
</apex:pageblocksectionitem>
<apex:pageblocksectionitem>
<apex:outputLabel value="{!$ObjectType.account.fields.billingcity.label}, {!$ObjectType.account.fields.billingstate.label}, {!$ObjectType.account.fields.billingpostalcode.label}" for="csz" />
<apex:outputText value="{!Account.BillingCity}, {!Account.BillingState} {!Account.BillingPostalCode}" id="csz"/>
</apex:pageblocksectionitem>
</apex:pageblocksection>
</apex:pageblock>
</apex:page>
code3

So by using $ObjectType.account.fields.billingcity.label (and similar markup for other objects and fields) we can allow org-specific customizations to come into our code with no effort required.

There's a catch (there always is). Note that there is no way to combine the three fields for City, State, and Zip (while using automatic renaming to help us with any customizations that the org has made) while maintaining proper labeling. In this case, the label for the combined field would be "Billing City, Billing State, Billing Zip/PostalCode." In other words, we can use whatever the org prefers for each field, but the developer must choose the label, which must remain static.

The more astute administrators and developers will note that there is a solution available: Use a component and pass the field labels displayed to the component. Then, using custom labels and translations, the component could be made truly international. This is, however, beyond the scope of this post. But it is an idea for a future post...

And the most astute amongst you are probably saying, "A truly flexible labeling system would allow the word "State" for a US address, "Province" for Canadian and others, and would still be completely customizable and translatable. Yes, that would be nice. :)

Comments

3 Tweets

2 Responses to “Flexible Field Labels”

  1. cvanderjagt on March 25th, 2009 11:26

    @dschach I like the post on flexible field labels: http://bit.ly/9Z2ko #salesforce #force

    This comment was originally posted on Twitter

  2. marplesoft on March 25th, 2009 12:56

    @dschach yeah that’s a good article on field labels http://bit.ly/9Z2ko

    This comment was originally posted on Twitter

Leave a Reply




Additional comments powered by BackType