Tuesday, June 30, 2015

Salesforce Custom Labels

In Visualforce Pages / custom Components, we might need to display static content. Also, in many places we will be displaying custom messages on screen, could be alerts / notes / error messages. In all these cases we can directly write the content in corresponding place holders. But the disadvantage of it is, to update the text we need to open the corresponding Visualforce or controller pages and edit them. Also, it doesn't support in multilingual applications i.e. doesn't have translations and if a person from different location logs in, this text language will not be changed.

To overcome these problems, and also maintain all messages at one place, Salesforce has provided 'Custom Labels'.

You can define a custom label for any message and can directly access it through apex code in controller or Visualforce page. We can also translate this to the languages supported by Salesforce.

To access custom labels, Go To Setup —> Create —> Custom Labels. All existing custom labels are displayed. Click on New Custom Labels.

A form will be opened. Provide all the required information and save it.

 Now you can see the newly created custom label. Also, in the bottom of screen, Translations section is available. Here we can provide the label's translation of all other languages this salesforce application / organization supports
 Click on 'New' button available in Translations section. Another form is opened, provide the translation and also select the language.


Now the label is available for usage and also it supports multi languages. 
You can access it throughapex and also visualforce page.

To access through Visualforce page, use global variable '$Label'
              In above example, it would be '{!$Label.Test_Item}'

To access through apex in controller, use 'System.Label'
              In above example, it would be 'System.Label.Test_Item'

Based on logged in user's language , the label text is displayed in corresponding translation.


No comments:

Post a Comment