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>
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>
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>
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. :)
Share Your Thoughts